Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix swagger.json not found in .net core 2.2 app

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.

like image 635
Mahesh More Avatar asked Jan 28 '19 17:01

Mahesh More


People also ask

How do I enable Swagger in .NET core?

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 .

How do I fix fetch Errorundefined Swagger v1 Swagger JSON?

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.

Where can I find Swagger JSON?

json). The Swagger UI can be found at http://localhost:<port>/swagger .


1 Answers

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"));                 

        }
like image 75
hhk Avatar answered Oct 12 '22 10:10

hhk