简体   繁体   中英

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.

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.

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

Try to use a relative path to SwaggerEndpoint:

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

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

"launchUrl": "swagger/index.html"

Finally it will be look like this:

在此处输入图像描述

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");
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM