5

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

KingKali
  • 51
  • 1
  • 3
  • When you say it's _"working fine"_ in Swagger, do you mean the routes are being documented, the endpoints are invokable, or both? With a bit more information, I can give you a complete answer. The `ApiVersionRouteConstraint` is registered through `IPostConfigureOptions` with the name configured in `ApiVersioningOptions.RouteConstraintName` which defaults to `apiVersion`. You've called `AddApiVersioning` so unless there is so other part of the routing system you've changed, it's hard to imagine it's not working. – Chris Martinez May 16 '22 at 16:25
  • I'm sorry, I've managed to fix it – KingKali Jan 06 '23 at 10:36
  • What did you do to fix it? – Jeremy Thompson Feb 27 '23 at 08:59

1 Answers1

3

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.

G.Dimov
  • 2,173
  • 3
  • 15
  • 40