.NET 5.0 中找不到 Swagger

Swagger not found in .NET 5.0

我有一个 .NET 5.0 API 项目设置 API 版本控制和 swagger。这是我的 ConfigureServices 方法:

services.AddSwaggerGen(c => {

    // Set the swagger doc stub
    c.SwaggerDoc("v1", new OpenApiInfo {
        Version = "v1",
        Title = "API"
    });

    // Set the comments path for the Swagger JSON and UI.
    string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    c.IncludeXmlComments(xmlPath);
});

这是我的 Configure 方法:

app.UseSwagger();
app.UseSwaggerUI(c => {
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
    c.RoutePrefix = string.Empty;
});

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints => {

    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller}/{action=Index}/{id?}");
});

我遇到的问题是,当我在本地 运行 项目时,/swagger 端点 returns 一个 404。但是,导航到 /swagger/v1/swagger.json returns swagger JSON 文档。我见过类似的问题 , and here, but none of the solutions presented here fixed the problem, mainly because I'm not using IIS. I've also looked at Microsoft's official documentation, here,但我没有注意到任何差异。我在这里做错了什么?

我通过查看 here 解决了这个问题。我不太清楚为什么,但是从 Configure 方法中删除行 c.RoutePrefix = string.Empty; 解决了这个问题。