Nuget引用

Swashbuckle.AspNetCore
安装 https://www.cnblogs.com/yilezhu/p/9241261.html
关键代码

services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Version = "v1",
Title = "EntityRiskUiBackendApi API",
Description = "EntityRiskUiBackendApi ASP.NET Core Web API",
Contact = new Contact
{
Name = "webname",
Email = string.Empty,
Url = "http://emergingriskanalytics.com/cn/index.aspx"
}
});
// 为 Swagger JSON and UI设置xml文档注释路径
var basePath = System.IO.Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径)
var xmlPath = System.IO.Path.Combine(basePath, xmlname);
c.IncludeXmlComments(xmlPath);
c.OperationFilter<SwaggerOperationFilter>(); // 手动高亮
});

启用中间件服务生成Swagger作为JSON终结点

app.UseSwagger();

启用中间件服务对swagger-ui,指定Swagger JSON终结点

app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "V1");
});

项目生成配置里
a
若要添加特殊的header

public class SwaggerOperationFilter : IOperationFilter
{
public void Apply(Operation operation, OperationFilterContext context)
{
operation.Parameters = operation.Parameters ?? new List<IParameter>();
var info = context.MethodInfo;
context.ApiDescription.TryGetMethodInfo(out info);
try
{
Attribute attribute = info.GetCustomAttribute(typeof(Microsoft.AspNetCore.Authorization.AuthorizeAttribute));
if (attribute != null)
{
operation.Parameters.Add(new BodyParameter
{
Name = "auth",
@In = "header",
Description = "access_token",
Required = true
});
}
}
catch
{ }
}
}