3

below are the version of NuGet is used.

PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.3.0" PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="2.2.0" PackageReference Include="Swashbuckle.AspNetCore" Version="3.0.0"

startup.cs

 services.AddApiVersioning(o =>
            {
                o.ReportApiVersions = true;
                o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
                o.AssumeDefaultVersionWhenUnspecified = true;
             o.ErrorResponses = new DefaultErrorResponseProvider();
            });

DemoTestController.cs

   [ApiVersion("2.0")]
        [ApiController]
        [Route("api/v{version:apiVersion}/[Controller]")]
        public class DemoTest : ControllerBase
        {
            [HttpGet]
            public IActionResult Get()
            {
                return new JsonResult(new { ResourceName = "DemoTestAPIController Version 2" });
            }
        }

for the above controller

http://localhost:53858/api/v2.0/DemoTest (this URL is valid and its response should be 200 and it is working as expected)

but for the below URL, the response should be 400(Forbidden). http://localhost:53858/api/v1.0/DemoTest

http://localhost:53858/api/v3.0/DemoTest

following are the expected responses for the respective api versoinig cases. Error Response https://github.com/Microsoft/aspnet-api-versioning/wiki/Error-Responses

Tried Till now: I also tried to override the DefaultError Response with MyErrorResponseProvider but the debugger does not hit it at all.

with the help of this asnswer

services.AddApiVersioning(o =>
            {
                o.ReportApiVersions = true;
                o.DefaultApiVersion = new Microsoft.AspNetCore.Mvc.ApiVersion(1, 0);
                o.AssumeDefaultVersionWhenUnspecified = true;
             o.ErrorResponses = new MyErrorResponseProvider();
            });

MyErrorResponseProvider

class MyErrorResponseProvider : DefaultErrorResponseProvider
{
// note: in Web API the response type is HttpResponseMessage
public override IActionResult CreateResponse( ErrorResponseContext context )
{
       switch ( context.ErrorCode )
       {
           case "UnsupportedApiVersion":
               context = new ErrorResponseContext(
                   context.Request,
                   context.StatusCode,
                   context.ErrorCode,
                   "My custom error message.",
                   context.MessageDetail );
               break;
       }

       return base.CreateResponse( context );
}
}

1 Answer 1

1

Below code work for me

I'm using version 3.1.6

<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="3.1.6" />

My startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddApiVersioning(options =>
            {
                options.ReportApiVersions = true;
                options.AssumeDefaultVersionWhenUnspecified = true;
                options.DefaultApiVersion = new ApiVersion(1, 0);
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

My controller

[ApiVersion("1.0")]
[ApiExplorerSettings(GroupName = "v1")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public class ValuesController : ControllerBase

If I try to use v2 I will get this error

error: {
  code: "UnsupportedApiVersion",
  message: "The HTTP resource that matches the request URI 'http://localhost:61273/api/v2/values' does not support the API version '2'.",
  innerError: null
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick response, I realized the following line was missing services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); this did the magic!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.