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
}
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
The following worked for me. I was able to get it working locally on IIS.
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");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With