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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With