I am struggling with the following situation:
I have an ASP.NET Core Web API with the following Swagger configuration in Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerDocument(config =>
{
config.PostProcess = document =>
{
document.Info.Version = "v1";
document.Info.Title = "...";
};
});
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseOpenApi();
app.UseSwaggerUi3();
...
}
If I run the api locally everything works fine. Means I can access the Swagger page at http://localhost:5000/swagger/index.html
and execute requests from there against the api which is available at http://localhost:5000/api
.
On production, my api is running inside a Docker container and behind a reverse proxy. The container exposes the Swagger page at http://localhost:5000/swagger/index.html
and the reverse proxy maps external requests to https://my.domain.com
to http://localhost:5000
internally. This way I am actually able to access the Swagger page at http://my.domain.com/swagger/index.html
. So far everything is fine.
The issue is when I want to execute reqests against my api from that production Swagger page. I see in the network tab of the browser dev tools that Swagger still sends the requests to http://localhost:5000/api
what of course does not work. Instead it should send the requests to https://my.domain.com/api
. Also below the title, the Swagger page states [ Base URL: localhost:5000 ]
.
I am not able to figure out how I could configure Swagger to use whatever base url I provide (like https://my.domain.com/
in my current case) so that I could execute requests against the api from anywhere and also that the BaseUrl below the title would be correct.
Can anybody help me here?
CodePudding user response:
Figured it out myself meanwhile. The solution is to remove the localhost server (which is added by ASP.NET Core middleware) and to add my own one like this:
app.UseOpenApi(settings =>
{
settings.PostProcess = (document, httpRequest) =>
{
document.Servers.Clear();
document.Servers.Add(new OpenApiServer { Url = "..." });
};
});
This Issue/Comment directed me in the right direction: https://github.com/RicoSuter/NSwag/issues/2441#issuecomment-583721522