Basic Configuration and description
Versioning helps in the timely rollout of functionality without destroying existing systems. It can also help provide additional functionality for selected customers. The API version can be done in different ways, such as adding a version to a URL or passing a custom header and using Accept-header as a query string parameter. In this article, we'll look at how to support multiple versions of the ASP. NET Core Web API
Create an ASP. NET Core Web API application. Install this package through NuGet: Microsoft.AspNetCore.Mvc.Versioning , open Startup.cs , modify ConfigureServices method, code as follows:
[Source] View Plain
- public void Configureservices (iservicecollection services)
- {
- Services. Addmvc ();
- Services. addapiversioning (option =
- {
- Option. Reportapiversions = true;
- Option. Assumedefaultversionwhenunspecified = true;
- Option. Defaultapiversion = New Apiversion (1, 0);
- });
- }
You can see that there are 3 different options configured:
ReportAPIVersions: This is optional. However, when set to True, the API returns supported version information in the response header. AssumeDefaultVersionWhenUnspecified: This option will be used to provide a request without a version. The assumed API version defaults to 1.0 DefaultApiVersion : This option specifies the default API version to use when no version is specified in the request. This will be the default version of 1.0
This is the configuration and settings. Now we'll see different ways to access the API version.
Via query string (by querying strings)
Open Controller the class, and then use the Apiversion property to decorate the control Controller class. Like this,
[Source] View Plain
- Namespace Multipleapiversions.controllers
- {
- [Apiversion ("1.0")]
- [Route ("Api/[controller]")]
- public class Valuescontroller:controller
- {
- [HttpGet]
- Public iactionresult Get () = OK (new string[] { "value1"});
- }
- }
The above version is set to 1.0, you can also set the API version to 2.0, for which you need to create another class with the same name in a different namespace Controller . Like this,
[Source] View Plain
- Namespace Aspnetcorewebapi.controllers2
- {
- [Apiversion ("2.0")]
- [Route ("Api/[controller]")]
- public class Valuescontroller:controller
- {
- [HttpGet]
- Public iactionresult Get () = OK (new string[] { "value2"});
- }
- }
Now go to the browser and access the controller. You should see the output of the API version 1.0 controller because the default access is version 1.0. Now append api-version = 2 in the URL, you should see the output of the API version 2.0 controller.
Via URL path Segment (via URL)
Query string parameters are useful, but can be painful in the case of long URLs and other query string parameters. Instead, a better approach is to add a version to the URL path. Like this
Api/v1/values api/v2/values
So to do this, we need to put the version in the route attribute:
[Source] View Plain
- Namespace Multipleapiversions.controllers
- {
- [Apiversion ("1.0")]
- [Route ("Api/v{version:apiversion}/[controller]")]
- public class Valuescontroller:controller
- {
- [HttpGet]
- Public iactionresult Get () = OK (new string[] { "value1"});
- }
- }
Again, you need to update the routing parameters to all requests. With this change, the API endpoint always needs to have a version number. You can api/v1/values change the version number in the URL by navigating to version 1.0, if you want to access version 2.0. Simple, looks cleaner
Via HTTP Headers (delivered via HTTP header)
In both of these methods, you need to modify the URL to support version control. However, if you want your API URLs to remain clean, the API version information can also be passed through an additional HTTP header. To make it work, you need to configure ApiVersionReader options
[Source] View Plain
- Services. addapiversioning (option =
- {
- Option. Reportapiversions = true;
- Option. Apiversionreader = New Headerapiversionreader ("api-version");
- Option. Defaultapiversion = New Apiversion (1, 0);
- Option. Assumedefaultversionwhenunspecified = true;
- });
Open postman Add header api-version Test
When you supply 2.0 as a value api-version , it calls version 2.0 Controller and returns the output
Easy to use settings. However, the query string parameter (parameter) will now not work correctly. headerafter you set it, you cannot specify a query string parameter (queries string parameter). If you wish to support it, please useApiVersionReader.Combine
[Source] View Plain
- Option. Apiversionreader = Apiversionreader.combine
- (
- New Querystringapiversionreader ("api-version"),
- New Headerapiversionreader ("api-version")
- );
Now, query string parameters and header both support
Keep in mind that we will also ReportApiVersions set to true return the version information in the response header. See
Now, let's take a look at a few other options.
Maptoapiversion
MapToApiVersionAttributes allow a single API action to be mapped to any version. In other words, support for multiple versions of a singleController
[Source] View Plain
- Namespace Multipleapiversions.controllers
- {
- [Apiversion ("1.0")]
- [Apiversion ("3.0")]
- [Route ("Api/v{version:apiversion}/[controller]")]
- public class Valuescontroller:controller
- {
- [HttpGet]
- Public iactionresult Get () = OK (new string[] { "value1"});
- [HttpGet, Maptoapiversion ("3.0")]
- Public iactionresult GetV3 () = OK (new string[] { "Value3"});
- }
- }
Deprecated (deprecated)
When multiple API versions are supported, some versions will eventually be eliminated. To indicate that one or more of the API versions will be deprecated, simply mark the API version that is ready to be deprecated. This does not mean that the API version is not supported, and these tagged APIs can still be called. This is just a way for users to realize that later versions will be discarded
[Apiversion ("1.0", Deprecated = True)]
Apiversionneutral (version neutral)
ApiVersionNeutralThe attribute defines that the API is version-neutral. This is useful for APIs that behave in exactly the same way, whether the API version is supported Controller or the API version is not supported Controller . Therefore, you can add ApiVersionNeutral attributes to exit version control
[Source] View Plain
- [Apiversionneutral]
- [Routeprefix ( "Api/[controller]")]
- Public class Sharedcontroller:controller
- {
- [HttpGet]
- Public iactionresult Get () = Ok ();
- }
Access version Information
If you want to know which version of the client is trying to access, then you can get that information from:
[Source] View Plain
- public string Get () = Httpcontext.getrequestedapiversion (). ToString ();
Support for multiple versions of the ASP. NET Core Web API