Versioning of your API’s in ASP.NET Core is easy thanks to the Microsoft.Aspnetcore.Mvc.Versioning nuget package. It allows us to implement versioning with a few configuration lines.
After installing the Nuget you should update the ConfigureServices() method in Startup.cs:
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers(); | |
services.AddApiVersioning(); | |
} |
Now you can add the [ApiVersion] attribute on your controller:
[ApiController] | |
[Route("api/[controller]")] | |
[ApiVersion("1.0")] | |
public class ExampleController : ControllerBase | |
{ | |
} |
If you now have multiple versions of your api, you can decide which version to use by adding an ?api-version=1.0
at the end of your url.
So far nothing new. A feature I only discovered recently is that you can also flag an API as deprecated. Therefore you need to set an additional Deprecated property on the attribute:
[ApiController] | |
[Route("api/[controller]")] | |
[ApiVersion("1.0",Deprecated = true)] | |
public class ExampleController : ControllerBase | |
{ | |
} |
When a client now calls the deprecated API, an extra response header ‘api-deprecated-versions’ is returned:
api-deprecated-versions: 1.0