C
C#2w ago
ero

API versioning and redirection to latest

Hi, I don't actually work on a backend at all. I'm just kinda curious how I would do this if I were to start working on a backend. Let's say I have a UsersController with 3 versions so far: v1.0, v1.1, and v2. My question is; can you set up a system where sending a request to /api/v1/users will default to the latest v1 version (v1.1), but where you can still explicitly send a request to /api/v1.0/users? Can I extend that further and additionally support sending a request to /api/latest/users (which would use v2)? In general, how would you tackle versioning in such a system? Do you version each individual endpoint? I guess you would version the entire controller, but only "override" the endpoints that need updating.
16 Replies
mg
mg2w ago
I've never dealt with this myself but I found this https://weblogs.asp.net/ricardoperes/asp-net-core-api-versioning Looks like you can do
[ApiController]
[ControllerName("Product")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("3.0")]
[ApiVersion("4.0")]
public class ProductVXController : ControllerBase
{
[HttpGet]
[MapToApiVersion("3.0")]
public ExtendedData GetV3()
{
//not important here
}

[HttpGet]
[MapToApiVersion("4.0")]
public ExtendedData GetV4()
{
//not important here
}
}
[ApiController]
[ControllerName("Product")]
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("3.0")]
[ApiVersion("4.0")]
public class ProductVXController : ControllerBase
{
[HttpGet]
[MapToApiVersion("3.0")]
public ExtendedData GetV3()
{
//not important here
}

[HttpGet]
[MapToApiVersion("4.0")]
public ExtendedData GetV4()
{
//not important here
}
}
ero
eroOP2w ago
https://github.com/dotnet/aspnet-api-versioning/wiki/API-Versioning-Options looks like this is a good resource as well but i don't think it really answers my core question, the redirection to the latest minor like i think in an ideal world i would inject IUsersService to UsersController, and then have V10.UsersService, V11.UsersService, V20.UsersService and then some mechanism to decide the version
mg
mg2w ago
I think I found a good solution
ero
eroOP2w ago
wait no i don't think you would version the service, the caller of the api doesn't care about that
mg
mg2w ago
The version redirecting isn't the responsibility of the endpoints/controllers, so you can write a request rewriter https://learn.microsoft.com/en-us/aspnet/core/fundamentals/url-rewriting?view=aspnetcore-9.0#url-redirect-and-url-rewrite
URL Rewriting Middleware in ASP.NET Core
Learn about URL rewriting and redirecting with URL Rewriting Middleware in ASP.NET Core applications.
mg
mg2w ago
Have a middleware that says "oh, they're requesting v1? let's see what the latest minor v1 is, and then just do the ol' switcheroo in the request, before it gets to the controllers"
ero
eroOP2w ago
ohh that looks perfect the examples do /v1/api. is that convention? i think i've only ever seen the other way around, /api/v1
mg
mg2w ago
My gut says /api/v1 is correct, as the version is a property of the API
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
this_is_pain
this_is_pain2w ago
you can also look at nick chapsas' videos about versioning
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
ero
eroOP2w ago
What repo? I just saw it in the images in the link mg posted
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
ero
eroOP2w ago
No description
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
this_is_pain
this_is_pain2w ago
this seem weird to me if a service is able to use v2 api then why ask v1, it should be the other way what i did for mine is that client asks service which api version it has

Did you find this page helpful?