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

6

19

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.

Rucker answered 15/7, 2019 at 15:55 Comment(0)
S
13

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.

Surmullet answered 15/7, 2019 at 16:24 Comment(2)
jbsmith - I commented out the RoutePrefix line. However, now instead of getting the chrome 404 page I am seeing the IIS 404 page (when navigating to {localhost}/swagger). Thoughts?Rucker
@JeremyS. I saw the answer for your question somewhere. You should call app.UseBaseUrl() or app.UseBasePath() for IIS.Wilk
O
10

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

Ody answered 16/7, 2019 at 7:7 Comment(0)
T
0

Try to use a relative path to SwaggerEndpoint:

 app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("../swagger/v1/swagger.json", "My API V1");
        });
Tantamount answered 4/8, 2022 at 11:34 Comment(0)
J
0

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");
    });
Jaggy answered 13/1, 2023 at 13:19 Comment(0)
I
0

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();
    });
Ida answered 6/3, 2023 at 22:10 Comment(0)
T
0
// 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();

 }
Tseng answered 24/7, 2023 at 6:26 Comment(2)
Answer needs supporting information Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Christos
although this answer is not actually the answer OP asked for, I realized that IsDevelopment does not work in IIS after seeing this.Outvote

© 2022 - 2024 — McMap. All rights reserved.