4

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?

staticdev
  • 2,950
  • 8
  • 42
  • 66

2 Answers2

6

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;
        });
Edward
  • 28,296
  • 11
  • 76
  • 121
  • 4
    this works (while `./` does not from the swagger documentation) but I'm just wondering is there any way to make swagger UI works for both IIS virtual directory hosting (like `staginghost/appname/swagger` and developers IIS express (like `localhost/swagger`) – oleksa Feb 11 '20 at 11:47
0

If you are using DTO with JsonProperty, you must check if there is any duplication in the property names.

 public class SampleDto
    {
        [JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
        public string Building { get; set; }

        [JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
        public string Comment { get; set; }

        [JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
        public string Country { get; set; }

}

In the code above the JsonProperty has "Comment" Twice as name. This will throw a 500 error.

By fixing the "Comment" json propertyname to "Country" like the dto propertyname, the code will function. (See adjusted code below)

public class SampleDto
    {
        [JsonProperty(PropertyName = "Building", NullValueHandling = NullValueHandling.Ignore)]
        public string Building { get; set; }

        [JsonProperty(PropertyName = "Comment", NullValueHandling = NullValueHandling.Ignore)]
        public string Comment { get; set; }

        [JsonProperty(PropertyName = "Country ", NullValueHandling = NullValueHandling.Ignore)]
        public string Country { get; set; }

}
Rodney Godfried
  • 219
  • 2
  • 4