AspnetCore 3.1 API giving UnsupportedApiVersion

Viewed 479

I have set up what I think is a nice structure for creating multiple versions of an API. I have decided to support versioning strictly via URI sigment (ie: /api/v1/records). I am also generating swagger docs for the API versions. The doc generation seems to be working fine but for some reason I am only able to reach my v1 endpoints. When I try to hit those everything works fine, but when I try to hit a v2 endpoint I am getting:

"Error": {
    "Code": "UnsupportedApiVersion",
    "Message": "The HTTP resource that matches the request URI 'https://localhost:5001/api/v2/records' is not supported.",
    "InnerError": null
}

I have created a simple reproduction of the problem in this repo. If you navigate to the /swagger page and try v1 or v2 you will see that v1 works but v2 does not. What am I missing here?

UPDATE

Ok I have made some progress in tracking down the issue. If I comment out the following line in ApdApiExtensions.cs: options.ApiVersionReader = new UrlSegmentApiVersionReader(); Then both versions of the API work and call the appropriate controller actions as long as I have both the URI version segment in the route AND the api-version query parameter set matching, for example: https://localhost:5001/api/v2/records?api-version=2

Obviously this is redundant so I added the line of code above to make the API use only URI segment versioning so I can forgo the query param. For some reason though this is breaking things and causing only v1 endpoints of the API to work.

1 Answers

Thanks Chaodeng for pointing me in the right direction. The answer is I had to add this to my controllers:

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

I did not need to comment out the UrlSegmentApiVersionReader though. Of course after adding that to the controller I have to update the routes in ApiRoutes.cs to remove the api/v# part so that it isn't duplicated but doing that seems to fix it.

Related