ASP.NET Core WebApi版本控制的实现

 在日常项目开发中,随着项目需求不断的累加、不断的迭代;项目服务接口需要向下兼容历史版本;前些时候就因为Api接口为做版本管理导致接口对低版本兼容处理不友好。

 最近就像了解下如何实现WebApi版本控制,那么版本控制有什么好处呢?

 WebApi版本控制的好处

有助于及时推出功能, 而不会破坏现有系统,兼容性处理更友好。

它还可以帮助为选定的客户提供额外的功能。

 接下来就来实现版本控制以及在Swagger UI中接入WebApi版本

一、WebApi版本控制实现 

 通过Microsoft.AspNetCore.Mvc.Versioning实现webapi 版本控制

创建WebApi项目,添加Nuget包:Microsoft.AspNetCore.Mvc.Versioning

Install-Package Microsoft.AspNetCore.Mvc.Versioning

修改项目Startup文件,使用Microsoft.AspNetCore.Mvc.Versioning

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //根据需要设置,以下内容 services.AddApiVersioning(apiOtions => { //返回响应标头中支持的版本信息 apiOtions.ReportApiVersions = true; //此选项将用于不提供版本的请求。默认情况下, 假定的 API 版本为1.0 apiOtions.AssumeDefaultVersionWhenUnspecified = true; //缺省api版本号,支持时间或数字版本号 apiOtions.DefaultApiVersion = new ApiVersion(1, 0); //支持MediaType、Header、QueryString 设置版本号;缺省为QueryString、UrlSegment设置版本号;后面会详细说明对于作用 apiOtions.ApiVersionReader = ApiVersionReader.Combine( new MediaTypeApiVersionReader("api-version"), new HeaderApiVersionReader("api-version"), new QueryStringApiVersionReader("api-version"), new UrlSegmentApiVersionReader()); }); services.AddControllers(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseHttpsRedirection(); //使用ApiVersioning app.UseApiVersioning(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }

WebApi设置版本:

  a)通过ApiVersion标记指定指定控制器或方法的版本号;Url参数控制版本(QueryStringApiVersionReader),如下:

namespace WebAPIVersionDemo.Controllers { [ApiController] [Route("[controller]")] //Deprecated=true:表示v1即将弃用,响应头中返回 [ApiVersion("1.0", Deprecated = true)] [ApiVersion("2.0")]public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[]{"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"}; [HttpGet] public IEnumerable<WeatherForecast> Get() { var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = $"v1:{Summaries[rng.Next(Summaries.Length)]}" }) .ToArray(); } } }

  通过参数api-version参数指定版本号;调用结果:

ASP.NET Core WebApi版本控制的实现

ASP.NET Core WebApi版本控制的实现

  b)通过Url Path Segment控制版本号(UrlSegmentApiVersionReader):为控制器添加路由方式如下,apiVersion为固定格式  

[Route("/api/v{version:apiVersion}/[controller]")]

  调用方式:通过调用路径传入版本号,如::5000/api/v1/weatherforecast

ASP.NET Core WebApi版本控制的实现

  c)通过Header头控制版本号:在Startup中设置(HeaderApiVersionReader、MediaTypeApiVersionReader)

apiOtions.ApiVersionReader = ApiVersionReader.Combine( new MediaTypeApiVersionReader("api-version"), new HeaderApiVersionReader("api-version"));

  调用方式,在请求头或中MediaType中传递api版本,如下:

ASP.NET Core WebApi版本控制的实现

ASP.NET Core WebApi版本控制的实现

其他说明:

    a)ReportApiVersions设置为true时, 返回当前支持版本号(api-supported-versions);Deprecated 参数设置为true表示已弃用,在响应头中也有显示(api-deprecated-versions):

ASP.NET Core WebApi版本控制的实现

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/dd54bf6deb3b9985b3797f7f6cca408c.html