Manoj Sethi
Manoj Sethi

Reputation: 2038

Issue with API Versioning .NET Core 2.2 UnsupportedApiVersion

I am creating an API and need to have versioning done in it. I am using package Microsoft.AspNetCore.Mvc.Versioning 3.1.3

My StartUp.cs is as follows

In ConfigureServices

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

        services.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

My 2 controllers on which I want to have versioning done are as below

namespace JWTWithRefresh.Areas.V1.CMS
{
    [AllowAnonymous]
    [ApiVersion("1.0")]
    [Route("api/[controller]/[action]")]
    public class AppController : Controller
    {
        public IActionResult GetApp()
        {
            return Ok("This is from API V1");
        }
    }
}

and another controller is as below

namespace JWTWithRefresh.Areas.V2.CMS
{
    [AllowAnonymous]
    [ApiVersion("2.0")]
    [Route("api/[controller]/[action]")]
    public class AppController : Controller
    {
        public IActionResult GetApp()
        {
        return Ok("This is from API V2");
        }
    }
}

The response I get when I make a call is as below

Endpoint = https://localhost:5001/api/App/GetApp?api-version=1.0

Response =

{
    "error": {
             "code": "UnsupportedApiVersion",
             "message": "The HTTP resource that matches the request URI 'https://localhost:5001/api/App/GetApp' is not supported.",
             "innerError": null
    }
}

Please guide me in fixing this issue if anyone has gone through the same.

Thanks

Upvotes: 3

Views: 6136

Answers (3)

The issue can come if you dont change the version from the dropdown in the swagger ui and in the api call, you change the version number and execute.

Make sure you change the version number from the dropdown swagger api version, before giving the api version number in the api call.

version in api call

Upvotes: 0

Water
Water

Reputation: 1182

For anyone else having the problem, I solved it by following suggestion from LGSon in comments above:

Solution 1:

Add [ApiController] in Controller

Solution 2:

Disable API Behavior

services.AddApiVersioning( options => options.UseApiBehavior = false );

Upvotes: 2

Abdullah İLTER
Abdullah İLTER

Reputation: 1

Change Route attribute like this

[Route("v{version:apiVersion}/[controller]/[action]")]

and endpoint = https://localhost:5001/api/App/v1/GetApp or https://localhost:5001/api/App/v2/GetApp.

for more: https://www.hanselman.com/blog/ASPNETCoreRESTfulWebAPIVersioningMadeEasy.aspx

Upvotes: 0

Related Questions