Home > Software engineering >  AspNetCore Swagger for JSON and UI endpoints returning 404
AspNetCore Swagger for JSON and UI endpoints returning 404

Time:02-21

I am trying to generate my Swagger documentation as per the Failed to load API definition

I have followed the Microsoft docs link as close as possible, and I don't see anything I potentially missed. Does anyone see what I'm doing wrong or missed?

(additional info: using .NET 5 and loading via Kestrel)

CodePudding user response:

Could you chage to SecuritySchemeType.ApiKey?

 services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1",
                new OpenApiInfo
                {
                    Version = "v1",
                    Title = "My API",
                    Description = "My API",
                    Contact = new OpenApiContact
                    {
                        Name = "User",
                        Email = string.Empty,
                        Url = new Uri("https://github.com"),
                    }
                });

            c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
            {
                Description = "Ex: Bearer {token here}",
                Name = "Authorization",
                Scheme = "Bearer",
                BearerFormat = "JWT",
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.ApiKey,
            });

            c.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference {Type = ReferenceType.SecurityScheme, Id = "Bearer"}
                    },
                    new string[] { }
                }
            });
        });

CodePudding user response:

I figured the issue, quite obscure if you ask me. Anyway, this StackOverflow answer helped me and was basically my issue.

When adding your SwaggerGen, the first parameter name

setupAction.SwaggerDoc(
    "Open API Specification for Haros v1", //... etc
)

slots into the URL for the generated documentation at the middleware part here:

setupAction.SwaggerEndpoint("/swagger/<from swagger doc name parameter>/swagger.json", "Haros API v1");

If you do this:

setupAction.SwaggerDoc(
            "v1", //... etc
)

And doing this:

setupAction.SwaggerEndpoint("/swagger/v1/swagger.json", "Haros API v1");

Swagger will load successfully on both endpoints (https://localhost:5001/swagger/index.html and https://localhost:5001/swagger/v1/swagger.json)

  • Related