2
votes

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?

1
What happens when you browse to localhost/mysite/swaggerDavid Yenglin
@DavidYenglin it doesn't match a route and give a blank screen.. the same screen I get if I enter: localhost/mysite/anythingstaticdev

1 Answers

4
votes

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