dotnet webapi 中添加Swagger文档

    首先添加"SwaggerGenerator": "1.1.0","SwaggerUi": "1.1.0" 需要注意的是这两个组件是我对Swashbuckle的重新封装,因为当前版本对泛型会报错。

    在ConfigureServices 中添加:

   

 1 services.ConfigureSwaggerGen(options =>
 2             {
 3                
 4                 options.SwaggerDoc("v1",
 5                   new Info
 6                   {
 7                       Version = "v1",
 8                       Title = "ryan API",
 9                   }
10               );
11                 options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Assembly.GetEntryAssembly().GetName().Name+".xml"));
12                 options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, Assembly.GetEntryAssembly().GetName().Name + ".Application.xml"));
13                 options.DescribeAllEnumsAsStrings();
14             });

在config方法中添加:

1  if (HostingEnvironment.IsDevelopment())
2             {
3                 app.UseSwagger();
4                 app.UseSwaggerUi(c=>c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs"));
5             }

在controller中需要添加attribute,否则文档会生成失败。

齐活,在浏览取中输入 http:{youhost}/swagger   即可访问了。

原文地址:https://www.cnblogs.com/ryansecreat/p/6094643.html