August 11, 2022 by Anuraj
AspNetCore DotNetCore MinimalApi
This post is about how to implement api versioning in ASP.NET Core 6.0 Minimal APIs. Earlier Minimal APIs versioning was not supported. Recently ASP.NET Core team introduced versioning in ASP.NET Core Minimal APIs. To implement it, first we need to create a Web API with Minimal API - we need .NET 6.0 or more to do this. Since I installed .NET 7 Preview versions, I am using the
--framework version parameter. We can create web api with the command like this -
dotnet new webapi -o WeatherForecastApi -minimal --framework net6.0. Once it is done, we need to add reference of
6.0.0-preview.3 using the command
dotnet add package Asp.Versioning.Http --version 6.0.0-preview.3.
Then the project file looks like this.
Next we need to modify the
Program.cs file and add the versioning support.
And we need to create an instance of version set, which will help add versions.
And finally apply the version set to the endpoints like this.
Now we are ready with the versioning support. Run the app and open the swagger endpoint and execute the GET request. Since we didn’t specified the version, we will get a BadRequest response.
We can fix this by accessing the
weatherforecast URL with
api-version=1.0 query string like this -
https://localhost:7208/weatherforecast?api-version=1.0. We can fix this issue by modifying
AddApiVersioning() with following parameters.
Since the Versioning options configured like
AssumeDefaultVersionWhenUnspecified we don’t need to pass
api-version the query string. And the
ReportApiVersions configured, the API will return the supported versions as Header.
I prefer the version information passed as header instead of query string. To do this, first we need to configure the
ApiVersionReader inside the
AddApiVersioning method like this.
Now we can invoke the GET request by sending a
api-version header. Here is an example -
curl --header "Api-Version: 1.0" https://localhost:7208/weatherforecast. And to support Open API / Swagger we need to implement
IOperationFilter interface and add the implementation to the
AddSwaggerGen method. Here is an implementation.
Next we need to update
AddSwaggerGen() method like this -
builder.Services.AddSwaggerGen(setup => setup.OperationFilter<ApiVersionOperationFilter>());. Once it is done, we can run the application and which will display the version parameter like this.
If we want to support specific version, we can do this using
MapToApiVersion() method like this.
This way we can enable support for versioning in Minimal APIs with ASP.NET Core 6.0. Similar to the Web API versioning, it support most of the versioning methods and features.
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