ASP.NET Core WebApi版本节制的实现(2)

    b)MapToApiVersion标志:答允将单个API操纵映射到任何版本(可以在v1的节制器中添加v3的要领);在上面节制器中添加以下代码,会见v3版本要领

[HttpGet] [MapToApiVersion("3.0")] public IEnumerable<WeatherForecast> GetV3() { //获取版本 string v = HttpContext.GetRequestedApiVersion().ToString(); var rng = new Random(); return Enumerable.Range(1, 1).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = $"v{v}:{Summaries[rng.Next(Summaries.Length)]}" }) .ToArray(); }

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

   c)留意事项:

    1、路径中参数版本高于,其他方法配置版本

    2、多种方法通报版本,只能回收一种方法通报版本号

    3、SwaggerUI中MapToApiVersion配置版本不会单独显示    

二、Swagger UI中版本接入

 1、添加包:Swashbuckle.AspNetCore、Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer  

//swaggerui 包 Install-Package Swashbuckle.AspNetCore //api版本 Install-Package Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer

 2、修改Startup代码:

public class Startup { /// <summary> /// Api版本提者信息 /// </summary> private IApiVersionDescriptionProvider provider; // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); //按照需要配置,以下内容 services.AddApiVersioning(apiOtions => { //返反响应标头中支持的版本信息 apiOtions.ReportApiVersions = true; //此选项将用于不提供版本的请求。默认环境下, 假定的 API 版本为1.0 apiOtions.AssumeDefaultVersionWhenUnspecified = true; //缺省api版本号,支持时间或数字版本号 apiOtions.DefaultApiVersion = new ApiVersion(1, 0); //支持MediaType、Header、QueryString 配置版本号;缺省为QueryString配置版本号 apiOtions.ApiVersionReader = ApiVersionReader.Combine( new MediaTypeApiVersionReader("api-version"), new HeaderApiVersionReader("api-version"), new QueryStringApiVersionReader("api-version"), new UrlSegmentApiVersionReader()); }); services.AddVersionedApiExplorer(option => { option.GroupNameFormat = "接口:'v'VVV"; option.AssumeDefaultVersionWhenUnspecified = true; }); this.provider = services.BuildServiceProvider().GetRequiredService<IApiVersionDescriptionProvider>(); services.AddSwaggerGen(options => { foreach (var description in provider.ApiVersionDescriptions) { options.SwaggerDoc(description.GroupName, new Microsoft.OpenApi.Models.OpenApiInfo() { Title = $"接口 v{description.ApiVersion}", Version = description.ApiVersion.ToString(), Description = "切换版本请点右上角版本切换" } ); } options.IncludeXmlComments(this.GetType().Assembly.Location.Replace(".dll", ".xml"), true); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //…… //利用ApiVersioning app.UseApiVersioning(); //启用swaggerui,绑定api版本信息 app.UseSwagger(); app.UseSwaggerUI(c => { foreach (var description in provider.ApiVersionDescriptions) { c.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant()); } }); //…… } }

 3、运行结果:  

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

其他: 

 示例地点:https://github.com/cwsheng/WebAPIVersionDemo

到此这篇关于ASP.NET Core WebApi版本节制的实现的文章就先容到这了,更多相关ASP.NET Core WebApi版本节制内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

您大概感乐趣的文章:

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

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