net5 webapi中 SwaggerUI如何进行版本控制
创建项目
net5就自带上了swaggerUI,见红色
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" }); }); } // 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.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1")); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } }
代码改造
1.添加FirstController
public class FirstController : ControllerBase { /// <summary> /// 这是V1版本的GetString /// </summary> /// <returns></returns> [HttpGet] [Route("api/[controller]/GetString")] public string GetToString() { return Newtonsoft.Json.JsonConvert.SerializeObject(new { Id = 123, Name = "闪电五连鞭" }); } /// <summary> /// 这是V1版本的GetString002 /// </summary> /// <returns></returns> [HttpGet] [Route("api/[controller]/GetString002")] public string GetString002() { return Newtonsoft.Json.JsonConvert.SerializeObject(new { Id = 123, Name = "闪电五连鞭" }); } /// <summary> /// 这是V1版本的Add /// </summary> /// <returns>name</returns> [HttpPost] [Route("api/[controller]/Add/{name:required}")] public string Add(string name) { return Newtonsoft.Json.JsonConvert.SerializeObject(new { Id = 234, Name = name }); } [HttpPut] [Route("api/[controller]/Update/{id:int}")] public int Update(int Id) { return Id; } [Route("api/[controller]/Update/{id:int}")] [HttpDelete] public int Delete(int Id) { return Id; } [Route("api/[controller]/Patch")] [HttpPatch] public int Patch() { return 123; } }
生成swagge有关项目的Xml注释文件,并修改其属性为“始终复制”
添加版本枚举类
public enum ApiVersions { V1 = 1, V2 = 2, V3 = 3, V4 = 4, V5 = 5 }
改造StartUp中swagger代码
public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { typeof(ApiVersions).GetEnumNames().ToList().ForEach(version => { c.SwaggerDoc(version, new OpenApiInfo() { Title = "Net5.WebAPI", Version = version, Description = $"Net5.WebAPI的 {version} 版本,可根据需要选择" }); }); #region 为Swagger JSON and UI设置xml文档注释路径 string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径) string xmlPath = Path.Combine(basePath, "Net5.WebAPI.xml"); c.IncludeXmlComments(xmlPath); #endregion // c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" }); }); } // 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.UseSwagger(); app.UseSwaggerUI(c => { typeof(ApiVersions).GetEnumNames().ToList().ForEach(version => { c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Net5.WebAPI {version}"); }); //c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1") }); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
添加版本号
在需要进行版本控制的控制器添加版本 [ApiExplorerSettings(GroupName = “V?”)]
大功告成,运行效果如下