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