I'm deploying my .net core app on IIS server and facing the issue in swagger UI where swagger.json not found. When I run it locally (Development environment) everything is working perfectly but when I deploy it on IIS server it fails to find swagger.json file.
Previously I was facing this issue in .net core 2.1 app and I resolved it by writing below code to get the virtual base path.
string basePath = Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");
basePath = basePath == null ? "/" : (basePath.EndsWith("/") ? basePath : $"{basePath}/");
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{basePath}swagger/v1/swagger.json", "Test.Web.Api");
c.RoutePrefix = "";
});
I have tried below code to resolve it:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint($"{env.ContentRootPath}swagger/v1/swagger.json", "Test.Web.Api");
//OR
c.SwaggerEndpoint($"{env.WebRootPath}swagger/v1/swagger.json", "Test.Web.Api");
c.RoutePrefix = "";
});
}
But abouve code didn't worked as it returns actual physical path and not virtual path.
- An output of the above code: C:/inetpub/wwwroot/myapp/swagger/v1/swagger.json
- Expected output: http://myserver/MySite/swagger/v1/swagger.json
Does anyone know how to get the virtual path in .net core 2.2 as Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");
is not working. Any lead would be helpful.