0

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?

Woody1193
  • 7,252
  • 5
  • 40
  • 90
  • Could you share us a hello world on github? I used swagger in netcore 3.1 without any problems – JRichardsz Oct 16 '21 at 05:57
  • @JRichardsz I'm not sure what you're asking for – Woody1193 Oct 16 '21 at 08:14
  • https://stackoverflow.com/help/how-to-ask section: hel to reproduce the error. If you reproduce the error in another minimal project, others could run your code and show you the error. Sometimes in the process of reproduce the error, you could find the error yourself. Swagger don't need database so is easy to configure. I'm shure that is a some settings or midleware – JRichardsz Oct 17 '21 at 13:44

1 Answers1

1

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.

Woody1193
  • 7,252
  • 5
  • 40
  • 90