'Default swagger index page not loading by default in Azure

I am working in a web api (.net 5.0) having swagger is enabled. Its working fine when running locally.

But after deploying into azure, its not loading the default page swagger/index.html by default. Instead page cannot be found (404) error is shown. But when I go to the page manually entering the URL its loading correctly and listing all the API end points.

So I went to the app service page and added a new default document in configuration like this

enter image description here

But nothing changed. Still the 404 error when we dont specify the swagger url.

What could be wrong??



Solution 1:[1]

One of the things i missed in the past , check your Startup.cs file especially the Configure() method.

Check if your code looks something like this:

if (env.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Lotus.API.Integration v1"));
}

Move the Swagger configuration lines outside the if statement and it should work

Solution 2:[2]

This solved the issue - no need to touch the default documents in configuration in app service.

I updated following code in dotnet 6 Program.cs for development and non-development environment separately.

app.UseSwagger();
if (app.Environment.IsDevelopment())
{
    app.UseSwaggerUI();
}
if (!app.Environment.IsDevelopment())
{
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
        options.RoutePrefix = string.Empty;
    });
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sajeetharan
Solution 2 Sanjeevi Subramani