Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why swagger.json gets not found error when hosted as an application under a website on IIS?

I tried to create a Webapi in Netcore 2.2 with both Swashbuckle.AspNetCore 5.0.0-rc2 and 4.0.1, the problem is the same. It works in local machine but when I compile a release version and deploy to IIS, I enter the site http://localhost/mysite/ and the error:

Fetch error Not Found /swagger/v1/swagger.json

Also, in the browser, if I enter http://localhost/mysite/swagger/v1/swagger.json I see a Json in OpenApi.

Very simple setup to reproduce:

  1. create a new webapi project.
  2. install swashbuckle.aspnetcore 5.0.0-rc2
  3. change the startup.cs:

public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });
    }

    // 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.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty;
        });

        app.UseMvc();
    }
}
  1. change the .csproj to have the directives:
 <Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
  </PropertyGroup>

  <!-- Enables XML comments on Swagger-UI -->
  <PropertyGroup>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <NoWarn>$(NoWarn);1591</NoWarn>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc2" />
  </ItemGroup>
 </Project>
  1. deploy to IIS and create an application pool.

Any idea of why the lib can't find the swagger.json that is there?

like image 517
staticdev Avatar asked Sep 18 '25 15:09

staticdev


1 Answers

For swagger.json, you need to append the website before the SwaggerEndpoint like c.SwaggerEndpoint("/mysite/swagger/v1/swagger.json", "My API V1");. As you have found, your swagger.json is exist under http://localhost/mysite/swagger/v1/swagger.json instead of http://localhost/swagger/v1/swagger.json.

Try to change your configuration like

        app.UseSwaggerUI(c =>
        {
#if DEBUG
               // For Debug in Kestrel
               c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
#else
               // To deploy on IIS
               c.SwaggerEndpoint("/mysite/swagger/v1/swagger.json", "Web API V1");
#endif
            c.RoutePrefix = string.Empty;
        });
like image 123
Edward Avatar answered Sep 21 '25 03:09

Edward