NETCORE - Swagger配置
环境:WebApi NET6
配置输出文档,项目右键-> 属性 -> 生成 -> 输出
勾选 “生成包含API文档的文件”
维护生成文件名称。
2. Program.cs 中的修改
把原来的
builder.Services.AddSwaggerGen();
修改成:
builder.Services.AddSwaggerGen(c => { //Authorization 鉴权 var scheme = new OpenApiSecurityScheme() { Description = "Authorization header. \r\nExample: 'Bearer abcdefxxx'", Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Authorization" }, Scheme = "oauth2", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey, }; c.AddSecurityDefinition("Authorization", scheme); var requirement = new OpenApiSecurityRequirement(); requirement[scheme] = new List<string>(); c.AddSecurityRequirement(requirement); //中文注释 var basePath = Path.GetDirectoryName(AppContext.BaseDirectory); c.IncludeXmlComments(Path.Combine(basePath, "swap.xml"), true); });
此时已增加了 鉴权 与注释
项目中 webapi 的类与接口需增加注释
项目发布后需要查看swagger时,需修改Program.cs里面的
if (app.Environment.IsDevelopment()) { } app.UseSwagger(); app.UseSwaggerUI();
参考:https://www.cnblogs.com/chenwolong/p/swger.html
完成!
标签:requirement,NETCORE,app,配置,var,new,Swagger,Authorization From: https://www.cnblogs.com/1285026182YUAN/p/17440635.html