在上一篇文章UWP/WinUI3 Win2d PixelShaderEffect 實現ColorPlacementEffect (顏色替換) 濾鏡。 - 吃飯/睡覺 - 博客園 (cnblogs.com)中實現了 ”顏色替換濾鏡“,那麼本文將製作一個“漸變映射濾鏡”。 效果圖: 一.漸變映射流程 1 ...
1.版本枚舉
/// <summary> /// swagger 多版本枚舉 /// </summary> public enum SwaggerVersionEnum { /// <summary> /// V1 版本,複雜情況可通過特性獲取 /// </summary> [Description("V1.000")] V1 = 1000, /// <summary> /// V2 版本,複雜情況可通過特性獲取 /// </summary> [Description("V2.000")] V2 = 2000, /// <summary> /// V2 版本,複雜情況可通過特性獲取 /// </summary> [Description("V3.000")] V3 = 3000, }
2.自定義特性,重寫路由
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ApiExplorer; using System; /// <summary> /// 自定義路由 /api/{version}/[controler]/[action] /// </summary> [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public class SwaggerVersionAttribute : RouteAttribute, IApiDescriptionGroupNameProvider { /// <summary> /// 分組名稱,是來實現介面 IApiDescriptionGroupNameProvider /// </summary> public string GroupName { get; set; } /// <summary> /// 自定義路由構造函數,繼承基類路由 /// </summary> /// <param name="actionName"></param> public SwaggerVersionAttribute(string actionName = "[action]") : base("/api/{version}/[controller]/" + actionName) { } /// <summary> /// 自定義版本+路由構造函數,繼承基類路由 /// </summary> /// <param name="actionName"></param> /// <param name="version"></param> public SwaggerVersionAttribute(SwaggerVersionEnum version, string actionName = "[action]") : base($"/api/{version.GetDescription()}/[controller]/{actionName}") { GroupName = version.GetDescription(); } }
3.介面使用特性
/// <summary> /// add2 /// </summary> /// <param name="dto"></param> /// <returns></returns> [HttpPost, SwaggerVersion(SwaggerVersionEnum.V2, "add")] public async Task<int> V2_Add([FromBody] ProductDto dto) { await Task.Run(() => { }); return 1; }
5.修改swagger配置
/// <summary> /// 添加服務: swagger /// </summary> /// <param name="services"></param> /// <returns></returns> public static void AddCustSwagger(this IServiceCollection services) { var version = "V1.0"; var apiName = "XXX系統"; // swagger多版本配置 var basePath = Directory.GetCurrentDirectory(); services.AddSwaggerGen(c => { //var versionList = typeof(SwaggerVersionEnum).GetEnumNames(); var versionList = EnumExtend.GetEnumList<SwaggerVersionEnum>().Select(x => x.Description); versionList.ToList().ForEach(version => { c.SwaggerDoc(version, new OpenApiInfo//"V1" { Version = version, Title = $"{apiName} 介面文檔", Description = $"{apiName} HTTP API {version}", //Contact = new OpenApiContact { Name = apiName, Email = "[email protected]", Url = new Uri("https://www.cnblogs.com/CRobot/") }, //License = new OpenApiLicense { Name = apiName, Url = new Uri("https://www.cnblogs.com/CRobot/") } }); c.OrderActionsBy(o => o.RelativePath); // 獲取應用程式xml所在目錄 var xmlPath = Path.Combine(basePath, "ProjectName.xml"); // swagger界面預設只顯示 方法&欄位 註釋,不顯示 控制器註釋 // 第二個參數為true, 則是controller的註釋 c.IncludeXmlComments(xmlPath, true); }); }); } /// <summary> /// 添加中間件: swagger /// </summary> /// <param name="app"></param> public static void UseCustSwagger(this IApplicationBuilder app) { app.UseSwagger(); app.UseSwaggerUI(options => { // swagger多版本配置 var apiName = "XXX系統"; //根據版本名稱倒序 遍歷展示 //var versionList = typeof(SwaggerVersionEnum).GetEnumNames(); var versionList = EnumExtend.GetEnumList<SwaggerVersionEnum>().Select(x => x.Description); versionList.OrderByDescending(e => e).ToList().ForEach(version => { options.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{apiName} {version}"); }); // 預設路徑為:/swagger/index.html // 路由首碼 - 設置為空,可直接跳轉到swagger頁面:/index.html // api測試可設置為空,部署時容易與前端路由衝突 options.RoutePrefix = string.Empty; }); }
Reference: