一、创建相关文件
- 创建一个Utility/SwaggerExt文件夹,添加一个类
二、在Program中找到Swagger相关配置信息
三、添加方法,在Program中调用
- 在SwaggerExt类中添加方法,将相关配置添写入
/// <summary>
/// swagger配置
/// </summary>
/// <param name="builder"></param>
public static void AddSwaggerExt(this WebApplicationBuilder builder )
{
builder.Services.AddSwaggerGen();
// 添加swagger注释
builder.Services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new OpenApiInfo { Title = "Swagger标题", Version = "第一版", Description = ".*** Core WebAPI中使用swagger版本控制,添加注释" });
var curr = AppContext.BaseDirectory;
x.IncludeXml***ments(curr + "/WebApplication1.xml", true);
});
}
/// <summary>
/// 使用Swagger
/// </summary>
/// <param name="app"></param>
public static void UseSwaggerExt(this WebApplication app)
{
app.UseSwagger();
app.UseSwaggerUI();
}
- 调用封装的方法
// 调用封装的Swagger配置
//SwaggerExt.AddSwaggerExt(builder);
builder.AddSwaggerExt();
if (app.Environment.IsDevelopment())
{
app.UseSwaggerExt();
}