I am currently working on an ASP.NET Core 7 Web API project and I am facing an issue with defining the API route using API versioning.
Program.cs
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Web APIs", Version = "v1" });
c.SwaggerDoc("v2", new OpenApiInfo { Title = "Web APIs", Version = "v2" });
});
builder.Services.AddApiVersioning(options =>
{
// Set the default version to 1.0
options.DefaultApiVersion = new ApiVersion(1, 0);
// Add support for versioning via URL segment
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
});
When I define the route in my controller like this:
[ApiController]
[ApiVersion("2.0")]
[Route("api/v2/Subscription")]
[Tags("Subscription")]
public class SubscriptionV2Controller : ControllerBase
I am getting the following error when trying to access the endpoint:
{
"Error": {
"Code": "UnsupportedApiVersion",
"Message": "The HTTP resource that matches the request URI 'https://localhost:7055/api/v2/Subscription' is not supported.",
"InnerError": null
}
}
However, if I modify the route as follows:
[ApiController]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/Subscription")]
[Tags("Subscription")]
public class SubscriptionV2Controller : ControllerBase
The endpoint works fine. The only issue is that the Swagger UI screen doesn't display the expected route.
I would like to have the route displayed correctly in the Swagger UI while still being able to use API versioning.


c.SwaggerDoc("v2", new OpenApiInfo { Title = "Web APIs", Version = "v2" });UnsupportedApiVersion