一、安装下图的两个nuget包
二、请求和返回的class都需要加上summary
包裹的注释头
三、program.cs添加配置
c#
builder.Services.AddSwaggerGen(c =>
{
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "swagger文档标题", Version = "v1" });
var sss = Assembly.GetExecutingAssembly();
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlModelFile = "model层名称.xml";
// 获取xml文件路径
// var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.CustomSchemaIds(x => x.FullName);
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFile), true);
c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlModelFile), true);
//c.IncludeXmlComments(xmlPath, true);
});
四、接口请求头配置
c#
/// <summary>
/// 接口名称
/// </summary>
/// <param name="参数名">注释</param>
/// <returns></returns>
[HttpPost]
[AllowAnonymous]
[SwaggerResponse(200, "成功", typeof(返回的class名))]
public ActionResult<返回的class名> getList([FromForm] 请求的class名 request)
{
return.......
}