Search code examples
iisswaggerasp.net-core-webapiswagger-uiswashbuckle.aspnetcore

After Deploying to IIS, Swagger UI page is not displayed


After deploying an ASP.NET Core Web API to IIS, Swagger UI page is not displayed.

Configure section:

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

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

Configure service:

services.AddSwaggerGen( );

Solution

  • Add c.RoutePrefix = string.Empty;, will change the start page.

    By default the url should be https://ip:port/swagger/index.html. And after you delpoyed this app, you will get 404 error, don't worry, you can append swagger in the url, it will show the index.html.

    enter image description here

    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();
        });
    }