这是我的仓库:https://github.com/FlameWolf/DotNet9WebApi
我的Program.cs
:
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOpenApi(options =>
{
options.OpenApiVersion = Microsoft.OpenApi.OpenApiSpecVersion.OpenApi3_0;
});
builder.Services.AddSwaggerGen(options =>
{
options.AddSecurityDefinition("bearerAuth", new()
{
Type = SecuritySchemeType.Http,
In = ParameterLocation.Header,
Scheme = "Bearer",
BearerFormat = "JWT",
Name = "Authorization",
Description = "Enter your bearer token"
});
options.AddSecurityRequirement(new()
{
{
new()
{
Reference = new()
{
Type = ReferenceType.SecurityScheme,
Id = "bearerAuth"
}
},
Array.Empty<string>()
}
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/openapi/v1.json", "v1");
});
app.MapGet("/", async context =>
{
await Task.Run(() => context.Response.Redirect("./swagger/index.html", permanent: false));
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
相同的调用曾经AddSwaggerGen
在 .NET 8 中起作用,其中我们使用AddEndpointsApiExplorer
而不是AddOpenApi
,并UseSwagger
使用 而不是MapOpenApi
。 Swagger UI 中将出现一个大而漂亮的“授权”按钮。但现在没有了。我该如何解决这个问题?
您可以使用如下所示的文档转换器:
1.确保安装该包
Microsoft.AspNetCore.Authentication.JwtBearer
2.
Program.cs
在下面更新您的点赞参考:使用文档转换器