部署到IIS后,不会显示Swagger UI页面

brccelvz  于 2023-08-05  发布在  其他
关注(0)|答案(2)|浏览(455)

将ASP.NET Core Web API部署到IIS后,不会显示Swagger UI页。
配置部分:

app.UseSwaggerUI(c =>
        {
            if (env.IsDevelopment() || env.IsProduction())

            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            }
        });

字符串
配置服务:

services.AddSwaggerGen( );

pprl5pva

pprl5pva1#

添加c.RoutePrefix = string.Empty;,将更改起始页。
默认情况下,url应该是https://ip:port/swagger/index.html。在你发布这个应用程序后,你会得到404错误,别担心,你可以在url中添加swagger,它会显示index.html。


的数据

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    ...
    services.AddSwaggerGen();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        if (env.IsDevelopment() || env.IsProduction())

        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            // Add this line, it will work for you
            c.RoutePrefix = string.Empty;
        }
    });

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

字符串

相关问题