REST API Versioning in AspNet Core - Part 1
These days REST APIs are used everywhere. As applications begin to mature, there are numerous times when APIs need to be updated. These need to be done in a systematic manner, ensuring that the existing consumer clients continue to work as usual without breaking the existing functionality. At the same time, it is necessary to update the new APIs and publish them.
This is where API Versioning plays a crucial role. If done properly, it can help avoid chaos and maintain consistency across different API integrations. As part of this article, I’ll explain the different versioning approaches that can be used for API versioning in ASP.Net Core.
There are 4 out of the box approaches to version APIs…
- Query string parameter
- URL path segment
- HTTP header
- Media type parameter
Of these, query string parameter is the default approach of versioning. One can also combine API versioning approaches together or define ones own custom method of API versioning. We’ll check each of these one by one. Also, we’ll use the default template of web api project that comes with aspnet core to understand each of these.
Creating a Web API project using ASPNet Core
Assuming you have AspNet Core installed on your machine, we’ll create a folder RESTAPIVersioning
and create a web api project within this using
Open the ValuesController.cs
file in this project. We’ll demonstrate the Query Parameter API versioning approach using this controller. To make this simpler, we’ll keep only the Get
action method in this controller and remove all the others.
Now, that this is done, we’ll start with the first approach of versioning, using Query string parameters.
1. API versioning using Query String Parameter
We’ll first install the NuGet package Microsoft.AspNetCore.Mvc.Versioning
which will help in API versioning.
To enable API versioning, add the following in ConfigureServices
method in Startup.cs
.
Now, ASP.Net Core provides a interface IApiVersionReader
which defines the behaviour of how an API version is read in its RAW unparsed form from the current HTTP request. For the Query string approach ASPNet Core provides QueryStringApiVersionReader
implementation out of the box which expects the query string parameter name as api-version
.
The default case can be configured in ConfigureServices
method as ….
If you plan to provide a custom api version parameter, say v
then,
We can now split the controllers by version numbers to support each api version.
We can now test the APIs using the query parameter api-version
as …
Both URLs, http://localhost:5000/api/values
and http://localhost:5000/api/values?api-version=1.0
give the response
This is because if api-version
is not specified the default version of 1.0
is considered.
While http://localhost:5000/api/values?api-version=2.0
gives the response
2. API versioning using URL path segment
In this approach, versioning is explicitly specified in the URL path itself. Here, it is not possible to have a default API version for a URL segment.
Lets create a new controller, to demonstrate this, say ColoursController.cs
.
Now, calling the API http://localhost:5000/api/v1.0/colours
would give the response
while, calling the API http://localhost:5000/api/v2.0/colours
would give
We could slightly update the above and also support version 3 in the 2nd controller (i.e. version the Action method) as …
With this, calling API http://localhost:5000/api/v3.0/colours
would give response…
3. API versioning using HTTP Header
In this case, we’ll have to provide the api version parameter as part of the HTTP request headers. Lets, understand this better using the CarsController as follows…
Now to enable HTTP header api versioning add the following in the ConfigureServices
method in Startup.cs
Unlike, Query parameter approach there is no standard or default HTTP header here. You will have to explicitly state this.
Now, calling the API http://localhost:5000/api/cars
with api-version = 1.0
in Request HTTP header would give the response
while, calling the API http://localhost:5000/api/cars
with api-version = 2.0
in Request HTTP header would give
4. API versioning using Media type parameter
Here we’ll have to pass the versioning parameter along with the media types for content negotiation. Lets create a MangoesController.cs
file to demonstrate this.
Now to enable Media Type api versioning add the following in the ConfigureServices
method in Startup.cs
Now, calling the API http://localhost:5000/api/mangoes
without any media type header gives the values..
while, calling the API http://localhost:5000/api/cars
with media type set to accept: text/plain;v=1.0
in Request HTTP header would give the same response.
Also, calling it with media type set to accept: text/plain;v=2.0;
would give…
With this we have covered all the widely used API versioning approaches. You may download the code from my github repository at this link. Hope this was useful!
Leave a Comment