'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



Solution 1:[1]

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 );

Solution 2:[2]

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

Solution 3:[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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Water
Solution 2 Abdullah ?LTER
Solution 3 Shameem Abdul Sathar