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.
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.
I have fixed my issue by putting below code in my .net core app.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api");
c.RoutePrefix = string.Empty;
});
}
As per swashbuckle documentation you need to prepend the ./ if you are hosting it in IIS.
I hope this answer will save your time!