In this article, we are going to cover an Asp Net Core 5 web API Versioning example. There are many approaches to versioning APIs. And in this article, we will see how to implement API versioning in Asp Net Core 5 web API application. So, first-of-all, we will create an Asp Net Core 5 web API and then we will see how to implement API versioning and different aspects of API versioning.
Implement API versioning in Asp Net Core 5.0 web API application
Let’s see how to implement API versioning in Asp Net Core 5 web API application in step by step.
Step # 1: Create Asp Net Core 5.0 Web API application in Visual Studio 2019.
First-of-all, now in this step we will create a new asp.net core 5 web api using visual studio 2019. So, go to Visual Studio 2019 and then click on the Create a new project and then select Asp.Net Core Web Application and then enter the name in the Project name field and then set location of project directory and then click on the Create button.
After clicking on the create button, then you will see a new popup as you do see below in the screenshot. Now, select Asp.Net Core 5.0 from the dropdown and then select Asp.Net Core Web API template and then click on the Create button.
After clicking on the Create button, then you will see a new Asp.Net Core 5.0 Web API project is created with the name of AspNetCore5WebAPIVersioning.
Step # 2: Create a new Controller
Now, in this step, we will add a new controller with the name of BookController. So, go to project folder structure and then right click on the Controllers folder and then select Add and then click on the Controller… and then select API from the left pane and then select the API Controller – Empty from the middle pane and then click on the Add button as you do see below in the screenshot.
Now, enter the name of the controller and then click on the Add button as you do see below in the screenshot.
Now, write the code as you do see below in the file.
Step # 3: Install Nuget Package
Now, in this step, we will add the NuGet package. So, go to Manage NuGet Packages for Solution and then search for “Microsoft.AspNetCore.Mvc.Versioning” and then install it as you do see below in the screenshot.
Step # 4: Configure API Versioning
Now, after installing the NuGet Package, then in this step, we will see how to configure API versioning in Asp Net Core 5 Web API application. So, go to Startup.cs class and then go to ConfiureServices method and then add the line as you do see below in the screenshot.
Now, run the project and then call enter the url (https://localhost:44385/api/Book) and then you will see the error as you do see below in the screenshot.
So, to solve this problem, we need to tell a default version if a specific version is not in the request. So, go to the Startup.cs class and then add the code as you do see below in the code file’s line # 6 and 7. So, this below code will help us to set a default API version in the request.
How to report all the supported versions?
So, in this step, we will see how to report all the supported versions. So, go to the Startup.cs class and then go to ConfigureServices method and then add the line of code as you do see below in the files’ line # 8.
Now, run the project and then call the API and then you will see the api-supported-versions as you do see below in the screenshot.
How to use the API supported versions in the controller?
Now, in this step, we will see how to implement API versions in Asp Net Core 5 web API controller. So, go to BookController and then write the attributes as you do see below in the file’s line # 12 and 13.
Now, run the project and then call the book api and then you will see all the supported versions as you do see below in the screenshot.
How to implement a deprecated version in API?
Now, in this step, we will see how to implement a deprecated version in Asp Net Core 5 web API application. So, just enter the extra parameter as you do see below in the screenshot’s line # 12.
How to pass API Version information in the HTTP Headers?
Now, in this step, we will see how to pass api version information in the https headers. So, go to Startup.cs file and then add the line of code as you do see below in the screenshot’s line # 36.
Now, run the project and then call the method with a specific version and then you will see the output as you do see below in the screenshot.
How to pass API Version information in the URL?
Now, in this step, we will see how to pass API Version information in the URL. So, go to the controller and then set the route as you do see below in the file’s line # 10.
Now, run the project and then call the url (https://localhost:44385/api/v1.0/Book) and then you will see the output as you do see below in the screenshot.
Thankyou for reading. Please keep visiting and sharing this blog within your community.
Save my night, thanks!