Mahesh More
Mahesh More

Reputation: 855

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.

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.

Upvotes: 14

Views: 40103

Answers (7)

oleksa
oleksa

Reputation: 4007

I've tried to use c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api"); from the Mahesh's answer but the problem is that it does not help.

I've got Swashbuckle sample started from VS using IIS Express

I'm getting error

Failed to load API definition. undefined ./swagger/v1/swagger.json

when was accessing the localhost/char/swagger endpoint from the web browser.

I had modified launchsettings.json\iisExpress\applicationUrl and added the path like "iisExpress": { "applicationUrl": "http://localhost/char" and Startup.cs source code like

   app.UseSwaggerUI(c =>
     {
      c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api");
      c.RoutePrefix = string.Empty;
    });

The solution was found at the Github issue please find it below:

app.UseSwaggerUI(c =>
{
    string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
    c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "My API");
});

I have not seen it in the documentation but it is working when

  • hosted by IIS Express locally with (or without) additional path after host name in the iisExpress\applicationUrl
  • hosted by IIS on staging environment (with additional path after hostname like http://staginghost/char/swagger

Upvotes: 6

Fadi Mounir
Fadi Mounir

Reputation: 41

i have faced this issue and found that you have to match the exact typing of Version in the path and both servicescollection and appbuilder

 services.AddSwaggerGen(c => {
    c.SwaggerDoc("v1" , new OpenApiInfo{ Title = "MyApp API" , Version = "v1" });
 });

 app.UseSwagger(); app.UseSwaggerUI(c => { 
    c.SwaggerEndpoint("/swagger/v1/swagger.json","MyApp API v1"); });

Upvotes: 4

Ehasanul Hoque
Ehasanul Hoque

Reputation: 640

In my cases I forgot to add HTTPGet Attribute one of my controller public get method

Upvotes: 2

hhk
hhk

Reputation: 548

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

        }

Upvotes: 6

Shadrach Muthiani
Shadrach Muthiani

Reputation: 1

For .Net Core 2.2

  // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSwaggerDocument();

then

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseOpenApi();
            app.UseSwaggerUi3();

        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

Upvotes: 0

Jayoti Parkash
Jayoti Parkash

Reputation: 878

You can fix the same in the following way:

Package: #region Assembly Swashbuckle.AspNetCore.Swagger, Version=4.0.1.0

Framework: .Net Core 2.2

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();
    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
         c.SwaggerEndpoint("/swagger/v1/swagger.json", "Versioned Api v1");
    );
}

This is work when you run the application locally or hosted on IIS.

Upvotes: 0

Mahesh More
Mahesh More

Reputation: 855

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!

Upvotes: 16

Related Questions