net5就自带上了swaggerUI,见红色

  1. // This method gets called by the runtime. Use this method to add services to the container.
  2. public void ConfigureServices(IServiceCollection services)
  3. {
  4. services.AddControllers();
  5. services.AddSwaggerGen(c =>
  6. {
  7. c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" });
  8. });
  9. }
  10. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  11. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  12. {
  13. if (env.IsDevelopment())
  14. {
  15. app.UseDeveloperExceptionPage();
  16. app.UseSwagger();
  17. app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1"));
  18. }
  19. app.UseRouting();
  20. app.UseAuthorization();
  21. app.UseEndpoints(endpoints =>
  22. {
  23. endpoints.MapControllers();
  24. });
  25. }
  26. }

1.添加FirstController

  1. public class FirstController : ControllerBase
  2. {
  3. /// <summary>
  4. /// 这是V1版本的GetString
  5. /// </summary>
  6. /// <returns></returns>
  7. [HttpGet]
  8. [Route("api/[controller]/GetString")]
  9. public string GetToString()
  10. {
  11. return Newtonsoft.Json.JsonConvert.SerializeObject(new
  12. {
  13. Id = 123,
  14. Name = "闪电五连鞭"
  15. });
  16. }
  17. /// <summary>
  18. /// 这是V1版本的GetString002
  19. /// </summary>
  20. /// <returns></returns>
  21. [HttpGet]
  22. [Route("api/[controller]/GetString002")]
  23. public string GetString002()
  24. {
  25. return Newtonsoft.Json.JsonConvert.SerializeObject(new
  26. {
  27. Id = 123,
  28. Name = "闪电五连鞭"
  29. });
  30. }
  31. /// <summary>
  32. /// 这是V1版本的Add
  33. /// </summary>
  34. /// <returns>name</returns>
  35. [HttpPost]
  36. [Route("api/[controller]/Add/{name:required}")]
  37. public string Add(string name)
  38. {
  39. return Newtonsoft.Json.JsonConvert.SerializeObject(new
  40. {
  41. Id = 234,
  42. Name = name
  43. });
  44. }
  45. [HttpPut]
  46. [Route("api/[controller]/Update/{id:int}")]
  47. public int Update(int Id)
  48. {
  49. return Id;
  50. }
  51. [Route("api/[controller]/Update/{id:int}")]
  52. [HttpDelete]
  53. public int Delete(int Id)
  54. {
  55. return Id;
  56. }
  57. [Route("api/[controller]/Patch")]
  58. [HttpPatch]
  59. public int Patch()
  60. {
  61. return 123;
  62. }
  63. }

生成swagge有关项目的Xml注释文件,并修改其属性为“始终复制”

 

 

 

 添加版本枚举类

  1. public enum ApiVersions
  2. {
  3. V1 = 1,
  4. V2 = 2,
  5. V3 = 3,
  6. V4 = 4,
  7. V5 = 5
  8. }

改造StartUp中swagger代码

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddControllers();
  4. services.AddSwaggerGen(c =>
  5. {
  6. typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
  7. {
  8. c.SwaggerDoc(version, new OpenApiInfo()
  9. {
  10. Title = "Net5.WebAPI",
  11. Version = version,
  12. Description = $"Net5.WebAPI的 {version} 版本,可根据需要选择"
  13. });
  14. });
  15. #region 为Swagger JSON and UI设置xml文档注释路径
  16. string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
  17. string xmlPath = Path.Combine(basePath, "Net5.WebAPI.xml");
  18. c.IncludeXmlComments(xmlPath);
  19. #endregion
  20. // c.SwaggerDoc("v1", new OpenApiInfo { Title = "Net5.WebAPI", Version = "v1" });
  21. });
  22. }
  23. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  24. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  25. {
  26. if (env.IsDevelopment())
  27. {
  28. app.UseDeveloperExceptionPage();
  29. app.UseSwagger();
  30. app.UseSwaggerUI(c =>
  31. {
  32. typeof(ApiVersions).GetEnumNames().ToList().ForEach(version =>
  33. {
  34. c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"Net5.WebAPI {version}");
  35. });
  36. //c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Net5.WebAPI v1")
  37. });
  38. }
  39. app.UseRouting();
  40. app.UseAuthorization();
  41. app.UseEndpoints(endpoints =>
  42. {
  43. endpoints.MapControllers();
  44. });
  45. }

在需要进行版本控制的控制器添加版本  [ApiExplorerSettings(GroupName = “V?”)]

 

 大功告成,运行效果如下

 

版权声明:本文为for-easy-fast原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/for-easy-fast/p/14493847.html