Jeremy S.
Jeremy S.

Reputation: 3171

Swagger 'swagger.json' loads, but 404 error on swagger UI '{localhost}/swagger' in AspNet project

Working on setting up swagger for a web application hosted with IIS using AspNetCore. The .json page loads and seems to be touching all the API just fine, however when navigating to {localhost}/swagger to view the UI page I receive a 404 error. I have the following code in Startup.cs:

//Configure Services


     services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "API ", Version = "v1" });
                c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
            });

            }


//Configure
            app.UseSwagger();
            app.UseStaticFiles();

            app.UseDeveloperExceptionPage();

            // Enable middleware to serve generated Swagger as a JSON endpoint.

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
            // specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("./swagger/v1/swagger.json", "My API V1");
                //c.RoutePrefix = string.Empty;
            });

            app.UseMvc();
        }

Does anyone see any potential issues with this? Or have any suggestions in terms of troubleshooting? Have been working on this for a while now, hopefully a fresh set of eyes can see something I'm not.

Upvotes: 20

Views: 44820

Answers (5)

Shakti Singh
Shakti Singh

Reputation: 21

// Configure the HTTP request pipeline.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();

 }

Upvotes: 1

Naod Agere
Naod Agere

Reputation: 59

I was supposed to change the following inside the Configure method of the Statrup class.

     app.UseEndpoints(endpoints =>
    {
        endpoints.MapRazorPages();
    });

To

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });

Upvotes: 0

Viktor
Viktor

Reputation: 636

For experiment try adding the next line in your ~\Properties\launchSettings.json file:

"launchUrl": "swagger/index.html"

Finally it will be look like this:

enter image description here

In Startup.cs remain app.UseSwaggerUI with the next parameters:

app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("v1/swagger.json", "API V1");
        c.InjectStylesheet("../swagger-ui/custom.css");
    });

Upvotes: 0

Ryan
Ryan

Reputation: 20116

Try to use a relative path to SwaggerEndpoint:

 app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
        });

And then navigate to {App Url}/swagger.

Refer to https://github.com/domaindrivendev/Swashbuckle/issues/971

Upvotes: 11

jbsmith
jbsmith

Reputation: 1666

From the MS Docs, you set the routePrefix to an empty string if you want the swagger UI to be served at the root of your web app.

To serve the Swagger UI at the app's root (http://localhost:/), set the RoutePrefix property to an empty string

The default is "/swagger"; it looks like you're overriding this, which is why the UI is not showing at "/swagger." Just hit "localhost:" and the UI should show up.

Remove that assignment of the routePrefix and it should show up at "/swagger" like you expect.

Upvotes: 13

Related Questions