In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to achieve version control in ASP.NET Core, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Install this package through NuGet: Microsoft.AspNetCore.Mvc.Versioning. Now, configure the service (services.AddApiVersioning ()) in the ConfigureServices () method of the Startup.cs class:
Public void ConfigureServices (IServiceCollection services) {services.AddMvc (); services.AddApiVersioning ();}
When you are using version 1.1.1 of Microsoft.AspNetCore.Mvc.Versioning, you only need to use the app.UseApiVersioning () method:
Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {/ * garbage code removed * / app.UseMvc (); app.UseApiVersioning ();}
Next, you must use ApiVersion to define the controller (multiple versions) that you want to support API versioning. Similarly, you must use MapToApiVersion to define a specific Action API version number:
[ApiVersion ("2.0")] [Route (" api/ [controller] ")] public class ValuesController: Controller {/ / GET api/values [MapToApiVersion (" 1.0")] [HttpGet] public IEnumerable Get () {return Json (new string [] {"value1", "value2"});}}
Now to call Action through GET, you must specify the API version and temporarily use the query string version control method. In this way, you will specify the API version directly in the query string. For example, this: http://localhost:5000/api/values?api-version=1.0.
If you add API version control to an existing API project, you can tell ASP.NET Core to treat the default controller and Action as version 1.0. To do this, configure the AddApiVersioning () service as follows:
Services.AddApiVersioning (options = > options.AssumeDefaultVersionWhenUnspecified = true)
Now you can call API with http://localhost:5000/api/values like this without causing any errors.
You can specify the API version in three ways:
Query characters (discussed)
URL path
Media type (Media Type)
Through the URL path, you can pass the version number as part of the URL path. An example like this is http://localhost:5000/api/v1/values. By the way, you must modify your routing properties to accommodate the version segment, as follows:
[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 the letter v is not forced to be added before the version number, it's just a convention.
Finally, you can configure the service to read the API version number from a specific media type (by default, you can configure your own media type to read from the content-type media type). Configure your service to activate media type versioning 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 a HTTP request, content-type the API version number in the request header, as follows (content-type: application/json;v=2.0):
By the way, with CurrentImplementationApiVersionSelector, if no version is defined in the content-type media type, the latest API version will be used. In the following example, I didn't mention any version number, so it uses the latest version in all versions.
The above is how to achieve version control in ASP.NET Core. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.