How to use API versioning in ASP.NET Core
June 20, 2024 by Anuraj
AspNetCore DotNet
In this blog post, we’ll learn how to implement API versioning with ASP.NET Core 8.0 Web API. I wrote a blog post long back on how to implement versioning in ASP.NET Core Minimal APIs. In this blog post we will learn how we can implement versioning in ASP.NET Core Web API with Controllers.
Creating ASP.NET Core Web API with controllers
First we need to create a ASP.NET Core Web API with controllers with the command dotnet new webapi --name WeatherForecast.Api --output WeatherForecast.Api\Src\WeatherForecast.Api --use-controllers
- this command will create a web api application, the --use-controllers
parameter will create the project with controllers, by default the web api project will be created with Minimal API.
In Visual Studio, when creating new project, we can select the Use controllers option, like this.
Once we create the project, we can add nuget package for versioning support and modify code in program.cs and controllers to enable versioning support.
Configure API versioning support
For configuring versioning support, we need to add the following nuget package - Asp.Versioning.Mvc.ApiExplorer
. We can add the using dotnet add package
command.
Next we need to modify the Program.cs
file like this.
Now if we run the application and browse the endpoint /WeatherForecast
we will get a BadRequest error - it is because we need to specify the version. To fix this error we can pass the query string like this - /WeatherForecast?api-version=1.0
- by default we can pass the version with query string. We can customize this with the ApiVersionReader
property. Here are the modified code.
In the code, we are configuring API Versioning. The ReportApiVersions
property setting to true, helps web api to display supported versions in the response headers. The DefaultApiVersion
property specifies the default API version to 1.0. And setting the AssumeDefaultVersionWhenUnspecified
property to true will help API developers to consume the APIs without specifying the version explicitly. The ApiVersionReader
property will help developers to specify version property - currently it is configured to provide version values as query string, HTTP Header or along with the media type.
Finally we need to decorate controller classes and action methods with the ApiVersion
attribute like this.
All the methods inside this controller supports both 1.0 and 2.0 versions. If we got a specific method which only available in version 2.0 - we can use the MapToApiVersion
attribute like this.
The GetWeatherForecastById
if we try to call this method without specifying the version will result a Bad Request error. Because this action method only available on version 2.0, and since we didn’t specified the version, the default version is applied which is 1.0.
We can also implement version deprecation using the following parameter in the ApiVersion
attribute like this.
This way we can configure versioning support in ASP.NET Core Web API. Next we will configure Open API to support versioned Web API endpoints.
Configure versioning support in Open API
To configure versioning in Open API, first we need to setup SwaggerDoc
for each of our version endpoints in the AddSwaggerGen
method. Like this.
Next we need to modify app.UseSwaggerUI
method like this.
Now if we run the application, in the swagger UI we will be able to see a dropdown list with all the versions and selecting the version, will load swagger.json
file and display it in the UI. Here is the screenshot.
And in each endpoint we will get option to send version value, like this.
In the above code snippet, we are hard coding the versions and OpenApiInfo objects. Instead of hard coding this we can implement IConfigureOptions<SwaggerGenOptions>
- which will enumerate the ApiVersionDescription
and configure the SwaggerDoc
dynamically like this.
And we can inject this to the runtime like this.
In this blog post, we learned about what is versioning, how to configure it for ASP.NET Core Web API with controllers, and how to use it with Swagger UI. Also, we learned about the way to add deprecated versions. The source code for this blog post available in GitHub
Happy Programming
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub