ToB企服应用市场:ToB评测及商务社交产业平台

标题: NetCore 配置Swagger [打印本页]

作者: 美丽的神话    时间: 2022-9-21 19:33
标题: NetCore 配置Swagger
1.添加Nuget
  1. install-package Swashbuckle.AspNetCore -project XXX -version 6.4.0
复制代码
2.添加静态类扩展方法

2.1.生成项目xml:选中项目 / 右键 / 属性 / 生成 / 输出 / 选中xml文档文件
2.2.system_v1:必须唯一不重复,且【options.SwaggerDoc("system_v1"】必须与【options.SwaggerEndpoint("/swagger/system_v1/】一致,不然会异常【Failed to load API definition; Fetch error: response status is 404 /swagger/system_v1/swagger.json】
  1. 1     /// <summary>
  2. 2     /// Swagger 静态类
  3. 3     /// </summary>
  4. 4     public static class SwaggerExtend
  5. 5     {
  6. 6         /// <summary>
  7. 7         /// 添加服务: swagger
  8. 8         /// </summary>
  9. 9         /// <param name="services"></param>
  10. 10         /// <returns></returns>
  11. 11         public static void AddCustSwagger(this IServiceCollection services)
  12. 12         {
  13. 13             var version = "V1.0";
  14. 14             var apiName = "XXX系统";
  15. 15             services.AddSwaggerGen(options =>
  16. 16             {
  17. 17                 options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
  18. 18            
  19. 19                 options.SwaggerDoc("system_v1", new OpenApiInfo
  20. 20                 {
  21. 21                     Version = version,
  22. 22                     Title = $"{apiName} API",
  23. 23                     Description = $"{apiName} {version} 接口服务"
  24. 24                 });
  25. 25
  26. 26                 //  获取应用程序所在目录
  27. 27                 var basePath = Path.GetDirectoryName(typeof(SwaggerExtend).Assembly.Location);
  28. 28                 var xmlPath = Path.Combine(basePath, "ProjectName.xml");
  29. 29                 //  swagger界面默认只显示 方法&字段 注释,不显示 控制器注释
  30. 30                 //  第二个参数为true, 则是controller的注释
  31. 31                 //options.IncludeXmlComments(xmlPath);
  32. 32                 options.IncludeXmlComments(xmlPath, true);
  33. 33             });
  34. 34         }
  35. 35
  36. 36         /// <summary>
  37. 37         /// 添加中间件: swagger
  38. 38         /// </summary>
  39. 39         /// <param name="app"></param>
  40. 40         public static void UseCustSwagger(this IApplicationBuilder app)
  41. 41         {
  42. 42             app.UseSwagger();
  43. 43             app.UseSwaggerUI(options =>
  44. 44             {
  45. 45                 options.SwaggerEndpoint("/swagger/system_v1/swagger.json", "系统API");
  46. 46                 //  默认路径为:/swagger/index.html
  47. 47                 //  路由前缀 - 设置为空,可直接跳转到swagger页面:/index.html
  48. 48                 //  api测试可设置为空,部署时容易与前端路由冲突
  49. 49                 options.RoutePrefix = string.Empty;
  50. 50             });
  51. 51         }
  52. 52     }
复制代码
3.StartUp注册服务,添加中间件
  1.         public void ConfigureServices(IServiceCollection services)
  2.         {
  3.             services.AddCustSwagger();
  4.         }
  5.         public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  6.         {
  7.             app.UseCustSwagger();
  8.         }
复制代码
 

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




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4