Swashbuckle.aspnetcore: .NET Core 2.2 and Swagger UI is not Displaying After Publish

Created on 8 Jul 2019  路  18Comments  路  Source: domaindrivendev/Swashbuckle.AspNetCore

hi
I use ASP Core 2.2
my config endpoint:
c.SwaggerEndpoint("/swagger/v1/swagger.json", "my api1");
Swagger works in locals

But Swagger does not show after the project's release (add in IIS)
(http://localhost:1404/swagger/index.html error page can鈥檛 be found)

but show json in url http://localhost:1404/swagger/v1/swagger.json

Swashbuckle.AspNetCore v4.0.1
Swashbuckle.AspNetCore.Swagger v4.0.1
Swashbuckle.AspNetCore.SwaggerGen v4.0.1
Swashbuckle.AspNetCore.SwaggerUI v4.0.1

Most helpful comment

Startup.cs change swagger sentence from env.IsDevelopment to outsite

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiClientes v1"));
app.UseHttpsRedirection()

All 18 comments

having the same issue here. All the files under /swagger folder are missing

/swagger/swagger-ui.css net::ERR_ABORTED 404 (Not Found)
/swagger/swagger-ui-bundle.js net::ERR_ABORTED 404 (Not Found)
/swagger/swagger-ui-standalone-preset.js net::ERR_ABORTED 404 (Not Found)
/swagger/swagger-ui-bundle.js net::ERR_ABORTED 404 (Not Found)
/swagger/swagger-ui-standalone-preset.js 404 (Not Found)

It also works fine in local for me. Core 2.2, Swashbuckle.AspNetCore v4.0.1 too.

Same issue. Any solution or work around?

I had this issue and it was due to this line in the UseSwaggerUI method:

c.RoutePrefix = string.Empty;

Try removing it if it's there, it worked for me.

I do have c.RoutePrefix = "api-docs/swagger"; in UseSwaggerUI, but removing it doesn't work either, the same error messages @minhSkoolbo posted after publishing.

Yup, setting correct RoutePrefix fixed my issue.
RoutePrefix was set to string.Empty and it was exactly what caused the problem.
Here's the corrected code in my case for your reference.

c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1");
c.RoutePrefix = "swagger";

@sbrfarhadi are you still experiencing this issue? If so, could you please provide an _exact_ set of repro steps. Without this I can't really investigate any further.

@domaindrivendev I'm still experiencing the issue with Core 2.2, Swashbuckle.AspNetCore v4.0.1. my set up:

    private const string RoutePrfix = "api-docs/swagger";
    private const string SwaggerEndpoint = "/swagger/v1/swagger.json";
    private const string SwaggerTitle = "My API";
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = SwaggerTitle, Version = "v1" });
    }
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.RoutePrefix = RoutePrfix;
        c.SwaggerEndpoint(SwaggerEndpoint, SwaggerTitle);
    });                

It works on my local, but after published, it shows 404 on static files as mentioned in prev posts.

@heqiao I suggest trying to match the route prefix and the prefix of the endpoint.
Either

private const string RoutePrfix = "swagger";
private const string SwaggerEndpoint = "/swagger/v1/swagger.json";

or

private const string RoutePrfix = "api-docs/swagger";
private const string SwaggerEndpoint = "api-docs/swagger/v1/swagger.json";

@minhSkoolbo
First one works in local but not after deployed just like before.
Second one failed locally saying "Fetch error Not Found api-docs/swagger/v1/swagger.json"

I deploy via kubernetes, don't think that makes a difference though. As it works fine with in local, with and without docker container.

@minhSkoolbo sorry for the late update, my issue was resolved.

        private const string RoutePrfix = "MY_CONTEXT_PATH/swagger";
        private const string RouteTemplate = "/swagger/{documentName}/swagger.json";
        private const string SwaggerEndpoint = "/swagger/v1/swagger.json";
        private const string SwaggerTitle = "RFP API";

        app.UseSwagger((c =>
        {
            c.RouteTemplate = RoutePrfix + RouteTemplate;
        }));
        app.UseSwaggerUI(c =>
        {
            c.RoutePrefix = RoutePrfix;
            c.SwaggerEndpoint($"/{RoutePrfix}{SwaggerEndpoint}", SwaggerTitle);
        });

it does not work when I install Swashbuckle.AspNetCore.Swagger, Swashbuckle.AspNetCore.SwaggerGen and Swashbuckle.AspNetCore.SwaggerUI separately. However, it works when I install Swashbuckle.AspNetCore only.

I my case the problem was with the IIS configuration itself: for newly created site, access to *.js and *.json files are not listed in Request Filtering feature. Once I added them, the problem has gone.

For me it was an issue with the following:

When I host locally, it looks like this:

https://localhost:44304/swagger/index.html

And the swagger.json file is located at:

https://localhost:44304/swagger/v1/swagger.json

When I host on IIS it looks like this:

https://myhost.domain.net/myapplication/swagger/index.html

And the swagger.json file is located at:

https://myhost.domain.net/myapplication/swagger/v1/swagger.json

But the code is looking for the swagger.json file at :

https://myhost.domain.net/swagger/v1/swagger.json

(It is using an absolute path instead of a relative one.)

Not sure how to fix this yet...

I had this error and fixed it by setting up the swagger middle-ware as follow:

app.UseSwaggerUI(c =>
{
       c.RoutePrefix = "v1";
       var basePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
       c.SwaggerEndpoint($"{basePath}/swagger/{c.RoutePrefix}/swagger.json", "Name");
});

Which is based on this post: https://stackoverflow.com/a/56282125/5830500

Scanning the thread, this issue looks to be resolved. If folks are still seeing related issues, please re-open a new ticket with clear repro steps

For me it was an issue with the following:

When I host locally, it looks like this:

https://localhost:44304/swagger/index.html

And the swagger.json file is located at:

https://localhost:44304/swagger/v1/swagger.json

When I host on IIS it looks like this:

https://myhost.domain.net/myapplication/swagger/index.html

And the swagger.json file is located at:

https://myhost.domain.net/myapplication/swagger/v1/swagger.json

But the code is looking for the swagger.json file at :

https://myhost.domain.net/swagger/v1/swagger.json

(It is using an absolute path instead of a relative one.)

Not sure how to fix this yet...

Adding the relative path to Swagger endpoint would fix it . Ex : c.SwaggerEndpoint("./swagger/v1/swagger.json", "V1");

I saw the actual error in the EventViewer. The XML file for XML comments was not in the publish folder. I copied it and it worked.

Startup.cs change swagger sentence from env.IsDevelopment to outsite

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiClientes v1"));
app.UseHttpsRedirection()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tibitoth picture tibitoth  路  3Comments

JoelAdamWeiss picture JoelAdamWeiss  路  4Comments

flipchart picture flipchart  路  4Comments

m-demydiuk picture m-demydiuk  路  3Comments

mrmartan picture mrmartan  路  3Comments