ASP. NET Core API version control, coreapi

Source: Internet
Author: User

[Switch] ASP. NET Core API version control, coreapi

A few days ago, my friends and I developed an API using ASP. NET Core, using the GET method to return some data to the client APP. We performed paging on the front end, which means we sent all the data to the client and then performed somedata.lengthOperation to obtainitems countUsed for paging logic. To reduce the load on HTTP requests, we decided to implement logic on the backend (server side paging. In this case, there is no problem because we can implement it quickly on the client. We modified all the logic on the client and server, and completed all the functions quickly.

However, you may have other clients in use because only one data source (API) is provided ). Introducing breakthrough changes in an API can support one client, but it also damages other clients. For example, assume that your mobile phone team is on vacation and your Web team is working to support the paging function on the server side. To support the Web team, you made a simple change in the API. You and your Web team are very happy with this change (if you are happy, clap your hands ). When the nightmare begins, you will find that millions of mobile clients do not work due to a very simple (Breakthrough) Change and users will uninstall the APP. When you find that you are not a mobile application developer and you do not have the source code access permission, it is a nightmare. Now you can only downgrade your APIs and Web applications, but the development team of Web applications is also on vacation. Because more nightmares will come one after another, and all things will be stuck.

Maybe (no, maybe, yes !) API version control is a very good choice in this case. Using API version control, you can not only securely address these breakthrough changes, but also support these changes, which is a win-win situation for everyone.

Let's take a look at how to configure the API version in ASP. NET Core.

Note:: I am using an empty ASP. NET Core Web API project (. NET Core 1.1)

Install this software package through NuGet: Microsoft. AspNetCore. Mvc. Versioning. NowStartup.csClassConfigureServices()Configure the Service (services.AddApiVersioning()):

public void ConfigureServices(IServiceCollection services){    services.AddMvc();    services.AddApiVersioning();}

When you are usingMicrosoft.AspNetCore.Mvc.VersioningOf1.1.1Version, you only need to useapp.UseApiVersioning()Method:

public void Configure(IApplicationBuilder app, IHostingEnvironment env,                       ILoggerFactory loggerFactory){    /* garbage code removed */    app.UseMvc();    app.UseApiVersioning();}

Next, you must useApiVersionDefine the Controller that supports API Version Control (multiple versions ). You must also useMapToApiVersionDefinitionSpecificActionAPI version:

[ApiVersion("2.0")][Route("api/[controller]")]public class ValuesController : Controller{    // GET api/values    [MapToApiVersion("1.0")]    [HttpGet]    public IEnumerable<string> Get()    {        return Json(new string[] { "value1", "value2" });    }}

Now passGETMethod callActionYou must specify the API version to use the query string version control method. In this way, you will directly specify the API version in the query string. For example, http: // localhost: 5000/api/values? Api-version = 1.0.

If you add API version control to an existing API project, you can notify ASP. NET Core to regard the default controller and Action as the version.1.0. To this end, configureAddApiVersioning()The service is as follows:

services.AddApiVersioning(options => options.AssumeDefaultVersionWhenUnspecified = true);

Now, you can call an api like http: // localhost: 5000/API/values without causing any errors.

You can specify the API version in three ways:

  • Query characters (discussed)
  • URL path
  • Media Type)

By using the URL path, you can pass the version number as part of the URL path. For example, http: // localhost: 5000/api/v1/values. By the way, you must modify your route attributes to adapt to the version segment, as shown below:

[ApiVersion("1.0")][Route("api/v{version:apiVersion}/[controller]")]public class ValuesController : Controller{    [HttpGet, MapToApiVersion("1.0")]    public IActionResult Get()    {        return Json(new string[] { "value1", "value2" });    }}

Note that lettersvIt is not mandatory to be added before the version number. This is just a convention.

Finally, you can configure the service to read the API version number from a specific media type (by default, read from the content-type media type, and you can configure your own media type. Configure your service to activate Media version control as follows:

public void ConfigureServices(IServiceCollection services){        // Add framework services.    services.AddMvc();    services.AddApiVersioning(options =>    {        options.ApiVersionReader = new MediaTypeApiVersionReader();        options.AssumeDefaultVersionWhenUnspecified = true;        options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);    });}

Now, when you send an HTTP requestcontent-typeSpecify the API version, as shown in the following figure (content-type: application/json; v = 2.0 ):

By the way, useCurrentImplementationApiVersionSelectorIfcontent-typeThe version defined in the media type. The latest API version is used. In the following example, I did not mention any version number, so it uses the latest version in all versions.

This is not all of the functions, there are other cool functions, you canMicrosoft's ASP. net api VersioningFind these features in the git Repository: https://github.com/microsoft/aspnet-api-versioning/wiki.

I found an article of the same type and explained it in more detail for your reference: http://www.talkingdotnet.com/support-multiple-versions-of-asp-net-core-web-api /.

Address: https://www.codeproject.com/Tips/1197505/ASP-NET-Core-API-Versioning-in-Simple-Words-Update
Translation: Sweet Tang
Address: http://www.cnblogs.com/tdfblog/p/asp-net-core-api-versioning.html
You are welcome to repost it. Please provide the source and link clearly.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.