5

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
iquellis
  • 979
  • 1
  • 8
  • 26

2 Answers2

4

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

Lis
  • 429
  • 4
  • 10
  • 1
    You are both right. @Chris Martinez had the implicit solution and Lis pointed out the concrete issue (I vote both up and set Lis' as the solution. – iquellis May 06 '19 at 16:52
3

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
Chris Martinez
  • 3,185
  • 12
  • 28
  • 1
    You are both right. @Chris Martinez had the implicit solution and Lis pointed out the concrete issue (I vote both up and set Lis' as the solution. – iquellis May 06 '19 at 16:52
  • 1
    Ah … I see. Sorry you stepped on that landmine @iquellis. A lot of people were not thrilled with what was required to remove UI controllers from the versioning policies. There was no perfect answer. The solution unfortunately resulted in a breaking behavior for some. Although it's called out in the documentation and release notes, it's easy to overlook. The wiki also describes how to change the default specifications should you want the old behavior. – Chris Martinez May 07 '19 at 17:10
  • Haven't been so important, thinking of old behavior, but thanks anyway. We are on a greenfield. Thanks again, bro. – iquellis May 08 '19 at 12:39
  • after adding [ApiController] methods not accepting parameters. All parameter are null after adding [ApiController] at controller. – Muhammad Saqib Aug 24 '20 at 16:02
  • @MuhammadSaqib, you'll have to provide more context or perhaps a separate question. There really is no difference between a UI and API controller anymore, but it's possible the `[ApiController]` attribute caused certain UI features to stop working as expected if you're mixing the two. – Chris Martinez Aug 25 '20 at 18:03
  • @ChrisMartinez I don't know may be but here is how I managed to solve it by adding `config.UseApiBehavior = false;` in `services.AddApiVersioning(config =>` in my startup file. No need to place [ApiController] decoration on all controllers. – Muhammad Saqib Aug 26 '20 at 13:39
  • @MuhammadSaqib that setting will enable API versioning on **all** controllers - UI and API. That's perfectly fine, but you should understand that behavior. In the past, service authors do not want versioning applied to UI controllers. This is addressed with **IApiControllerFilter**, which you can customize if you want. The default filter looks for `[ApiController]`. `UseApiBehavior = false` essentially reverts to the behavior before this feature was added (2+ years ago). – Chris Martinez Aug 27 '20 at 18:12
  • @ChrisMartinez The project I'm working on is designed as UI and API controllers are mixed for example sometimes UI calls API controllers and sometimes API calls UI controllers. – Muhammad Saqib Aug 28 '20 at 04:27