Nswag: UseSwaggerUi3: SwaggerEndPoint

Created on 30 Jan 2019  路  6Comments  路  Source: RicoSuter/NSwag

Hello,

I'm using NSwag.AspNetCore 12.0.13 version and all works great when I run in local machine. But, When I deploy code to Server with New Virtual Directory under IIS/DefaultSite/NewSite it gives me error Failed to load API definition.(Swagger/v1/swagger.json).

It seems I need to configure the setting in startup.cs file under configure but I couldn't find a way to do it.

I'm using .net core 2.1 and Nswag.AspNetCore 12.0.13 version.

Any help is appreciated.

question

All 6 comments

Any IIS logs/exceptions etc?

Hi @andycshi, I was having a similar issue with my asp.net core app being hosted in a virtual directory. I modified the app.UseSwaggerUi3 call to reflect this and it now works.

app.UseSwaggerUi3(config => config.TransformToExternalPath = (internalUiRoute, request) =>
{
    if (internalUiRoute.StartsWith("/") == true && internalUiRoute.StartsWith(request.PathBase) == false)
    {
       return request.PathBase + internalUiRoute;
    }
    else
    {
        return internalUiRoute;
    }
});

Source: https://github.com/RSuter/NSwag/wiki/AspNetCore-Middleware#hosting-as-an-iis-virtual-application

Hi Wesley,

Thanks for your input.

I resolved it using X-Forwarded-Host. Which gives the original host requested by the client and I'm applying swagger path.

`

  app.UseSwagger(
               settings =>
               {                 
                   settings.Path = "/swagger/" + "v1" + "/swagger.json";
                   settings.PostProcess = (document, request) =>
                   {
                       document.Host = request.Headers["X-Forwarded-Host"].FirstOrDefault();

                   };
               });

            app.UseSwaggerUi3(options =>
            {
                options.DocumentPath = "/swagger/" + "v1" + "/swagger.json";
            });

`

Thanks for looking in to it.

If this is something others could be interested in, it would be great to update the wiki:
https://github.com/RSuter/NSwag/wiki/AspNetCore-Middleware#hosting-as-an-iis-virtual-application

Can we close this issue?

Can we close this issue?

Yes, Thank you !

Was this page helpful?
0 / 5 - 0 ratings