.NET Api Version - Format Route

226 views Asked by At

I have an API controller with multiple versions included in the routing like:

[ApiVersion("1.0-AB")]
[ApiVersion("2.0-AB")]
[Route("v{version:apiVersion}/controller/action")]

This matches the following routes:

  • 1.0-AB/controller/action
  • 2.0-AB/controller/action

However, I'd like to only include the major and minor api versions in the route, so the possible routes become:

  • 1.0/controller/action
  • 2.0/controller/action

I need to keep the status part of the api version (-AB), so this can't be removed. Is there a way of achieving this in .NET Core?

1

There are 1 answers

0
Chris Martinez On

The short answer is - no; certainly not easily and that's by design.

It's not clear what you hope to you achieve and why you would want to do this. Either the status is part of your API version and included, or it's not. There shouldn't be anything hidden or obscure to the client in terms of an API version. The client must ask the server for the version it wants and the server is responsible for serving the request or saying that it can't. At no time should the server try to guess what the client wants or pull the rug out from underneath it by using a version other than what was asked for.

It would be like if the client asked for application/json, but the server returned application/xml. The server doesn't know that the client can handle that. The same is true with an API version. A contract is a contract. There is no such thing as backward capability. A server cannot guarantee that all clients will be able to changes, even if it's a simple as adding or removing attributes from data.

If the status is meant to track or otherwise identify something that only the server knows about, then there is probably a better way to achieve that. If you can elaborate more about your goals in the question, I can update the answer with more comprehensive guidance. Depending on your specific scenario, there are some ways you can influence the incoming API version even that's generally not a good idea.