1.Startup.cs中ConfigureServices方法中添加:
授权鉴权内容
#region 授权鉴权
//Bearer 的scheme定义
var securityScheme = new OpenApiSecurityScheme()
{
Description = "使用JWT方案授权,请求时,在请求头部信息中加入: \"Authorization: {token}\"",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "",
BearerFormat = "JWT"
};
var securityRequirement = new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "token"
}
},
new string[] {}
}
};
#endregion
swagger设置内容:
services.AddSwaggerGen(options =>
{
//获取接口方法注释文件地址
var xmlPath = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
//设置swagger版本,标题,描述
options.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1", Description = "description." });
//是否显示注释
options.IncludeXmlComments(xmlPath, true);
//设置实体模型注释
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "YiSha.Entity.xml"));
//设置授权鉴权
options.AddSecurityDefinition("token", securityScheme);
options.AddSecurityRequirement(securityRequirement);
});
2.设置注释项目的.csproj文件中设置该节点为True
3.控制器方法添加注释:
4.实体模型添加注释: