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