// ...
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
// ...
if (app.Environment.IsDevelopment()) // by default enabled only for dev.
{
app.UseSwagger();
app.UseSwaggerUI();
}
var builder = WebApplication.CreateBuilder(args);
//Add the Swagger generator to the services collection
builder.Services.AddSwaggerGen();
字符串 使用JWT授权配置Swagger的示例如下:
builder.Services.AddSwaggerGen(option =>
{
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Demo API", Version = "v1" });
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
In = ParameterLocation.Header,
Description = "Please enter a valid token",
Name = "Authorization",
Type = SecuritySchemeType.Http,
BearerFormat = "JWT",
Scheme = "Bearer"
});
option.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[]{ }
}
});
});
2条答案
按热度按时间but5z9lq1#
如果您将从正确的模板创建一个项目,它已经带有Swagger预设置。例如,您可以使用ASP.NET Core Web API模板(通过IDE或使用带有
webapi
模板名称的dotnet new
command)。它包括(对于.NET 7):字符串
Program.cs
为单位):型
另请阅读:
Swashbuckle.AspNetCore
:入门v8wbuo2f2#
.NET 7 WebAPI项目,您需要在
Program.cs
文件上配置它。您需要将Swagger生成器添加到服务集合中。
字符串
使用JWT授权配置Swagger的示例如下:
型