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.
Add and configure Swagger middlewareLaunch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .
The "Console" tab shows the file where the problem originated from (v1/swagger/json:1). Opening it by clicking it showed that one of the helper methods I used in my controller was "Public". Changing it to "Private" fixed the problem for me.
json). The Swagger UI can be found at http://localhost:<port>/swagger .
For .net core 5, swagger is added automatically on project creating.
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyProject.Api", Version = "v1" });
});
}
However, you need to make sure that two commented lines are out of the if block.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider svp)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//app.UseSwagger();
//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProject.Api v1"));
}
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProject.Api v1"));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With