iquellis
iquellis

Reputation: 1053

Unsupported API version with versions within URI — what's wrong?

I have a Web API project using .net core 2.2 (maybe there's the problem.)

The routes are awaiting OAuth authorization with OpenIdDict, but that works totally fine for me. I am trying a very simple approach:

The startup.cs just contains:

services.AddApiVersioning();

The API controller has three different routes for test purposes. Notice that the controller itself has no [Route()] or [ApiVersion()] annotations.

[HttpGet]
[Authorize]
[ApiVersion("2.0")]
[Route("api/v{version:apiVersion}/vt")]
public IActionResult GetVt20()
{
    return Ok("2.0");
}

[HttpGet]
[Authorize]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/vt")]
public IActionResult GetVt10()
{
    return Ok("1.0");
}

If I do an authorized request like

http://localhost:27713/api/v1.0/vt

.net core answers with a BadRequest:

{"error":{"code":"UnsupportedApiVersion","message":"The HTTP resource that matches the request URI 'http://localhost:27713/api/v1.0/vt' does not support the API version '1.0'.","innerError":null}}

What am I missing?

Upvotes: 5

Views: 18531

Answers (2)

Lis
Lis

Reputation: 428

I had the same issue when migrating from 2.1 net core to 2.2 Just add to your controller class [ApiController] Attribute

Github Issue

Upvotes: 4

Chris Martinez
Chris Martinez

Reputation: 4418

Are you using the latest versions of the libraries? Are you using Endpoint Routing or Legacy Routing? What does the rest of your configuration look like? With the limited information you've provided, I don't see an immediate reason why it won't work.

Here is a working example based on the information you've provided:

[ApiController]
public class VTController : ControllerBase
{
    [HttpGet]
    [ApiVersion( "1.0" )]
    [Route( "api/v{version:apiVersion}/[controller]" )]
    public IActionResult Get( ApiVersion apiVersion ) =>
        Ok( new { Action = nameof( Get ), Version = apiVersion.ToString() } );

    [HttpGet]
    [ApiVersion( "2.0" )]
    [Route( "api/v{version:apiVersion}/[controller]" )]
    public IActionResult GetV2( ApiVersion apiVersion ) =>
        Ok( new { Action = nameof( GetV2 ), Version = apiVersion.ToString() } );
}

The following routes resolve as expected:

  • http://localhost/api/v1/vt
  • http://localhost/api/v2/vt

Upvotes: 4

Related Questions