Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 979
Jeremy S. Avatar asked Jul 15 '19 15:07

Jeremy S.


2 Answers

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.

like image 98
jbsmith Avatar answered Sep 16 '22 12:09

jbsmith


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

like image 32
Ryan Avatar answered Sep 17 '22 12:09

Ryan