Swagger UI not displaying when deploying API on IIS
Solution 1
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()
Solution 2
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
Pedro Leal
Updated on June 27, 2022Comments
-
Pedro Leal almost 2 years
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 }
-
Hoppeduppeanut almost 4 yearsWhile this might answer the question, you should edit your answer to include an explanation of how this code block answers the question. This makes your answer much more useful to those who come across the same issue later on.
-
Luke_ about 3 yearsYou didnt add the image correctly, use ![description](link) instead
-
EduardoUstarez over 2 yearsIt works to me if (env.IsDevelopment() || env.IsProduction()), thanks
-
c-sharp-and-swiftui-devni over 2 yearsThis answer just made me feel like a flipping idiot well spoted why on earth does boiler plate code add it here.
-
rtfmpliz over 2 yearsit works for me in Net6 minimal API if (app.Environment.IsDevelopment() || app.Environment.IsProduction()) { app.UseSwagger(); app.UseSwaggerUI(); }