I have a .NET 5.0 API project setup with API versioning and swagger. This is my ConfigureServices
method:
services.AddSwaggerGen(c => {
// Set the swagger doc stub
c.SwaggerDoc("v1", new OpenApiInfo {
Version = "v1",
Title = "API"
});
// Set the comments path for the Swagger JSON and UI.
string xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
string xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
and this is my Configure
method:
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1");
c.RoutePrefix = string.Empty;
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
The problem I'm having is that, when I run the project locally, the /swagger
endpoint returns a 404. However, navigating to /swagger/v1/swagger.json
returns the swagger JSON document. I've seen similar problems here, here and here, but none of the solutions presented here fixed the problem, mainly because I'm not using IIS. I've also looked at Microsoft's official documentation, here, but I haven't noticed any differences. What am I doing wrong here?
I was able to fix the issue by looking at here. I'm not quite sure why, but removing the line c.RoutePrefix = string.Empty;
from the Configure
method fixed the issue.