Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 = "[email protected]"
          }
      });

        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
}
like image 448
Pedro Leal Avatar asked Jun 15 '20 21:06

Pedro Leal


Video Answer


3 Answers

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

image

like image 194
Adeshina Alani Avatar answered Oct 06 '22 10:10

Adeshina Alani


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

like image 9
vipul nanavare Avatar answered Oct 06 '22 09:10

vipul nanavare


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

 });

like image 1
Novice in.NET Avatar answered Oct 06 '22 09:10

Novice in.NET