KingKali
KingKali

Reputation: 91

Constraint Reference 'ApiVersion' could not be resolved to a type

Constraint Reference Exception:

Constraint Reference Exception

Hi, I'm using .Net 6 Web API with versioning, my versioning works just fine in Swagger, but when I reference my API from MVC Framework (.NET 6), I'm getting an exception that says :

InvalidOperationException: The constraint reference 'apiVersion' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'.

    public static IServiceCollection AddApiVersioningConfig(this IServiceCollection services)
    {
        services.AddApiVersioning(cfg =>
        {
            cfg.DefaultApiVersion = new ApiVersion(1, 0);
            cfg.AssumeDefaultVersionWhenUnspecified = true;             // In case if the user doesn't specify the version, so we assume to use the default one (v1)
            cfg.ReportApiVersions = true;                               // This will mention which API the user is currently using (Header).
           

            // 1-  api/v1/clients/ => In order to read the segment that contains the version eg.

            // 2- api-version : 1.0 => In case the user provides the version as header  

            // 3- ?api-version=1.0 => From query approach

            cfg.ApiVersionReader = ApiVersionReader.Combine(
                new HeaderApiVersionReader("X-version"),
                new QueryStringApiVersionReader("api-version"),
                new UrlSegmentApiVersionReader(),
                new MediaTypeApiVersionReader("ver"));
        });

        return services;
    }
}

app.UseSwaggerUI(opt =>
{
    var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();

    foreach (var description in provider.ApiVersionDescriptions)
    {
        opt.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",  description.ApiVersion.ToString());
    }
});

Many thanks for your help :)

KingKali

Upvotes: 9

Views: 10284

Answers (3)

Henkie85
Henkie85

Reputation: 245

The problem could be that you are using the wrong nuget package. You have Asp.Versioning.WebApi and you have Asp.Versioning.Mvc

If you use the original MVC controllers for your Api, you need to use Asp.Versioning.Mvc, not Asp.Versioning.WebApi.

https://www.nuget.org/packages/Asp.Versioning.Mvc/ https://www.nuget.org/packages/Asp.Versioning.WebApi/

Upvotes: 0

Shareef DotNet Guru
Shareef DotNet Guru

Reputation: 41

Add below code in program.cs

.AddApiVersioning()
.AddVersionedApiExplorer(options =>
 {
    options.GroupNameFormat = "'v'VV";
    options.SubstituteApiVersionInUrl = true;
 })

Upvotes: 2

G.Dimov
G.Dimov

Reputation: 2393

I was receiving the same error on my Web API that is deployed on Azure App Service but didn't receive the error locally.

Error: InvalidOperationException: The constraint reference 'apiVersion' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'.

Solution:

In Startup.cs file I've added the following line:

services.AddApiVersioning();

That method is located in Microsoft.Extensions.DependencyInjection.

After redeploying on our Azure App Service with that change, everything was working as expected.

Upvotes: 13

Related Questions