简体   繁体   中英

Swagger UI not displaying when deploying API on IIS

Well, I'm using Swagger for my API documentation and it works perfectly in localhost, the problem begins when I host it on the IIS. For somereason it just doesn't work anymore

localhost:

https://localhost:44381/swagger/index.html

api:

http://200.155.29.14/SimuladorFrete/Swagger/index.html

All I get when I try to open the Swagger after deploying it in the ISS is a blank page and a 500 internal server error, which doesn't say the exception.

Here is my Configure method (startup.cs)

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();

    }
    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
    app.UseSwagger();
    app.UseStaticFiles();
    app.UseSwaggerUI(c =>
    {
        if (env.IsDevelopment())
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
        }
        else
        {
            // To deploy on IIS
            c.SwaggerEndpoint("/SimulaFrete/swagger/v1/swagger.json", "Web API V1");
        }

    });
}

and here's my ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson();

    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1",

      new OpenApiInfo
      {

          Title = "Simulador de frete por Unidade",
          Version = "v1",
          Description = "Métodos da API de simulação de frete",
          Contact = new OpenApiContact
          {
              Name = "Catalde Technology",
              Url = new Uri("https://www.catalde.com"),
              Email = "cicero.catalde@catalde.com"
          }
      });

        string caminhoAplicacao =
            PlatformServices.Default.Application.ApplicationBasePath;
        string nomeAplicacao =
            PlatformServices.Default.Application.ApplicationName;
        string caminhoXmlDoc =
            Path.Combine(caminhoAplicacao, $"{nomeAplicacao}.xml");

        c.IncludeXmlComments(caminhoXmlDoc);
        c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); //This line
    });
}

And there's my Controller code which has only this method:

[HttpPost]
public ContentResult Post([FromBody]dadosFrete xml)
{
    // code logic
}

You need to temporarily add the production clause in your condition before you can see the swagger in the production environment. See the yellow highlighted section in the attached image. env.IsProduction()

图片

Try this. To set relative path.

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

    });

if this solution not work. Then check publish setting. The issue is that running dotnet publish with -r Release does not produce XML file. However, dotnet publish with -r Debug does in fact produce the file. This explains why people are only getting this issue when they are deploying to environments OTHER than locally. You can simplify find out in Project > Your Project properties > Build Tab

Take a look at how to use multiple environments in .net core. Some options are

  • using a launch.json file for your different environments (dev/stage/prod) (local dev only)
  • Adding an environment variable named ASPNETCORE_ENVIRONMENT with development , stage or production
  • Or making a web.config to deploy to IIS and add the value
<PropertyGroup>
<EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

For IIS, it all depends on the version of IIS. Older versions of IIS, I believe you can only go with web.config.

Simply edit your csproj file and add the following snippet

<PropertyGroup>
 <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

The following worked for me. I was able to get it working locally on IIS.

  1. You'll need to publish the project to a folder and point IIS to that folder.
  2. Edit the Configure method and move out Swagger out of env.IsDevelopment and use vipul's solution above to fix the path.
 if (env.IsDevelopment())
 {
   app.UseDeveloperExceptionPage();
 }
 app.UseSwagger();
 app.UseSwaggerUI(c =>
 {
   string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
   c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "Web API");

 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM