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

29,103

Solution 1

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!

Solution 2

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

        }

Solution 3

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 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

Solution 4

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

Solution 5

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

Share:
29,103
Mahesh More
Author by

Mahesh More

Updated on July 05, 2022

Comments

  • Mahesh More
    Mahesh More almost 2 years

    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.

  • Mahesh More
    Mahesh More about 5 years
    Yup, but I missed "." before "/". Thanks!
  • oleksa
    oleksa about 4 years
    unfortunately this does not work. I have application hosted using IIS express and had set launchsettings.json as "iisExpress": { "applicationUrl": "http://localhost/char", to test IIS virtual directory. However the problem is that localhost/char/swagger shows 404 error but localhost/char/swagger/v1/swagger.json works (the json file is shown). localhost/swagger works fine if there is no path in the applicationUrl value. I'm using netcore31 and Swashbuckle.AspNetCore 5.0.0 package
  • oleksa
    oleksa about 4 years
    @VAAA yes I did. Please check my answer in this thread
  • Basel Issmail
    Basel Issmail almost 4 years
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
  • oleksa
    oleksa over 3 years
    @GabrielSimas this solution is two years old. Try to post new Swashbuckle.WebApi issue it you beleive that did it properly.
  • Aweda
    Aweda almost 3 years
    This solution saved my life!