0

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.

enter image description here

I would like to have the route displayed correctly in the Swagger UI while still being able to use API versioning.

enter image description here

4
  • you did not add version2 c.SwaggerDoc("v2", new OpenApiInfo { Title = "Web APIs", Version = "v2" });
    – jeb
    CommentedMay 19, 2023 at 10:11
  • @jeb I added this, but still same issue. Api responding with error code UnsupportedApiVersionCommentedMay 19, 2023 at 10:23
  • 1
    Hmm,I am not sure. I have a working app with 2 versions but there are many parts not shown in your sample. So i can't pin the exact problem. Maybe this link can help you. referbruv.com/blog/… or someone else can answer it.
    – jeb
    CommentedMay 19, 2023 at 10:34
  • Thanks! @jeb, The URL which you have shared it helps me to resolve my issue.CommentedMay 19, 2023 at 14:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.