NetCore Swagger 多版本

种地  金牌会员 | 2022-9-23 22:08:06 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 952|帖子 952|积分 2856

1.版本枚举
  1.     /// <summary>
  2.     /// swagger 多版本枚举
  3.     /// </summary>
  4.     public enum SwaggerVersionEnum
  5.     {
  6.         /// <summary>
  7.         /// V1 版本,复杂情况可通过特性获取
  8.         /// </summary>
  9.         [Description("V1.000")]
  10.         V1 = 1000,
  11.         /// <summary>
  12.         /// V2 版本,复杂情况可通过特性获取
  13.         /// </summary>
  14.         [Description("V2.000")]
  15.         V2 = 2000,
  16.         /// <summary>
  17.         /// V2 版本,复杂情况可通过特性获取
  18.         /// </summary>
  19.         [Description("V3.000")]
  20.         V3 = 3000,
  21.     }
复制代码
2.自定义特性,重写路由
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  3. using System;
  4.     /// <summary>
  5.     /// 自定义路由 /api/{version}/[controler]/[action]
  6.     /// </summary>
  7.     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
  8.     public class SwaggerVersionAttribute : RouteAttribute, IApiDescriptionGroupNameProvider
  9.     {
  10.         /// <summary>
  11.         /// 分组名称,是来实现接口 IApiDescriptionGroupNameProvider
  12.         /// </summary>
  13.         public string GroupName { get; set; }
  14.         /// <summary>
  15.         /// 自定义路由构造函数,继承基类路由
  16.         /// </summary>
  17.         /// <param name="actionName"></param>
  18.         public SwaggerVersionAttribute(string actionName = "[action]") : base("/api/{version}/[controller]/" + actionName)
  19.         {
  20.         }
  21.         /// <summary>
  22.         /// 自定义版本+路由构造函数,继承基类路由
  23.         /// </summary>
  24.         /// <param name="actionName"></param>
  25.         /// <param name="version"></param>
  26.         public SwaggerVersionAttribute(SwaggerVersionEnum version, string actionName = "[action]") : base($"/api/{version.GetDescription()}/[controller]/{actionName}")
  27.         {
  28.             GroupName = version.GetDescription();
  29.         }
  30.     }
复制代码
3.接口使用特性
  1.         /// <summary>
  2.         /// add2
  3.         /// </summary>
  4.         /// <param name="dto"></param>
  5.         /// <returns></returns>
  6.         [HttpPost, SwaggerVersion(SwaggerVersionEnum.V2, "add")]
  7.         public async Task<int> V2_Add([FromBody] ProductDto dto)
  8.         {
  9.             await Task.Run(() => { });
  10.             return 1;
  11.         }
复制代码
5.修改swagger配置
  1.         /// <summary>
  2.         /// 添加服务: swagger
  3.         /// </summary>
  4.         /// <param name="services"></param>
  5.         /// <returns></returns>
  6.         public static void AddCustSwagger(this IServiceCollection services)
  7.         {
  8.             var version = "V1.0";
  9.             var apiName = "XXX系统";
  10.             //  swagger多版本配置
  11.             var basePath = Directory.GetCurrentDirectory();
  12.             services.AddSwaggerGen(c =>
  13.             {
  14.                 //var versionList = typeof(SwaggerVersionEnum).GetEnumNames();
  15.                 var versionList = EnumExtend.GetEnumList<SwaggerVersionEnum>().Select(x => x.Description);
  16.                 versionList.ToList().ForEach(version =>
  17.                 {
  18.                     c.SwaggerDoc(version, new OpenApiInfo//"V1"
  19.                     {
  20.                         Version = version,
  21.                         Title = $"{apiName} 接口文档",
  22.                         Description = $"{apiName} HTTP API {version}",
  23.                         //Contact = new OpenApiContact { Name = apiName, Email = "swagger@xxx.com", Url = new Uri("https://www.cnblogs.com/CRobot/") },
  24.                         //License = new OpenApiLicense { Name = apiName, Url = new Uri("https://www.cnblogs.com/CRobot/") }
  25.                     });
  26.                     c.OrderActionsBy(o => o.RelativePath);
  27.                     //  获取应用程序xml所在目录
  28.                     var xmlPath = Path.Combine(basePath, "ProjectName.xml");
  29.                     //  swagger界面默认只显示 方法&字段 注释,不显示 控制器注释
  30.                     //  第二个参数为true, 则是controller的注释
  31.                     c.IncludeXmlComments(xmlPath, true);
  32.                 });
  33.             });
  34.         }
  35.         /// <summary>
  36.         /// 添加中间件: swagger
  37.         /// </summary>
  38.         /// <param name="app"></param>
  39.         public static void UseCustSwagger(this IApplicationBuilder app)
  40.         {
  41.             app.UseSwagger();
  42.             app.UseSwaggerUI(options =>
  43.             {
  44.                 //  swagger多版本配置
  45.                 var apiName = "XXX系统";
  46.                 //根据版本名称倒序 遍历展示
  47.                 //var versionList = typeof(SwaggerVersionEnum).GetEnumNames();
  48.                 var versionList = EnumExtend.GetEnumList<SwaggerVersionEnum>().Select(x => x.Description);
  49.                 versionList.OrderByDescending(e => e).ToList().ForEach(version =>
  50.                 {
  51.                     options.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{apiName} {version}");
  52.                 });
  53.                 //  默认路径为:/swagger/index.html
  54.                 //  路由前缀 - 设置为空,可直接跳转到swagger页面:/index.html
  55.                 //  api测试可设置为空,部署时容易与前端路由冲突
  56.                 options.RoutePrefix = string.Empty;
  57.             });
  58.         }
复制代码
 
Reference:

NetCore 配置Swagger
   

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

种地

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表