.net 如何使dotnet API入口点直接登陆到swagger文档

ldioqlga  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(153)

我为dotnet swagger gen设置了以下配置:

builder.Services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo
        {
            Title = "Demo",
            Version = "v1",
            Description = "This is a demo api"
        });

        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
    });

有没有什么方法可以配置,以便当用户转到localhost:port/ lands时直接转到swagger docs?

mzsu5hc0

mzsu5hc01#

是的,您需要更改swagger UI的配置:

app.UseSwaggerUI(options =>
{
    options.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    options.RoutePrefix = "";
});

相关问题