Nswag: Base URL for Swagger UI behind a load balancer/proxy

Created on 21 Dec 2017  路  2Comments  路  Source: RicoSuter/NSwag

We have started moving our public API to the Swagger framework and decided to use NSwag to do that. There is one issue with our Swagger UI though. As the public API always lives behind a proxy, the base url is always wrong (it always points to localhost instead of the public url), which means that the UI can only be used as documentation and not as an actually interactive service where you can try out requests.

Is there a way to set the base url for UI only?

This is how we define (and start) the swagger UI:

  app.UseMvc();      
  var swaggerUiSettings = new SwaggerUiSettings
  {
    DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase,
    DefaultEnumHandling = NJsonSchema.EnumHandling.String,
    IsAspNetCore = true,
    Title = "API",
    Version = "v1",
    SwaggerUiRoute = ""
  };

  app.UseSwaggerUi(typeof(Startup).Assembly, swaggerUiSettings);
question

All 2 comments

You can change the host, scheme, etc. with the PostProcess callback of the SwaggerUiSettings

Thanks, adding this solved the issue

    PostProcess = document =>
    {
      document.Host = "my.public.address";
      document.Schemes = new System.Collections.Generic.List<SwaggerSchema> { SwaggerSchema.Https };
    }

image

Was this page helpful?
0 / 5 - 0 ratings