Swagger UI How to generate documentation

Cenk 956 Reputation points
2023-03-31T14:53:12.61+00:00

Hi there,

In my ASP.NET Core 6 Web API application I am using Swagger UI. I wonder if I can somehow generate a PDF document from Swagger? Or any other ways to do it?

Thank you.

builder.Services.AddSwaggerGen(c =>
{
    c.EnableAnnotations();
    c.SwaggerDoc("v1", new OpenApiInfo
    {
        Title = "Palas API", 
        Version = "v1",
        Description = "Online Game Code Transactions",
        Contact = new OpenApiContact
        {
            Name = "Palas Support",
            Email = "destek@destek.com"
            
        }

    });
    // Set the comments path for the Swagger JSON and UI.
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    c.IncludeXmlComments(xmlPath);
    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        In = ParameterLocation.Header,
        Description = "Please enter token",
        Name = "Authorization",
        Type = SecuritySchemeType.Http,
        BearerFormat = "JWT",
        Scheme = "bearer"
    });
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
    {
        {
            new OpenApiSecurityScheme
            {
                Reference = new OpenApiReference
                {
                    Type=ReferenceType.SecurityScheme,
                    Id="Bearer"
                }
            },
            new string[]{}
        }
    });
});

//builder.Services.Configure<ApiBehaviorOptions>(options
//    => options.SuppressModelStateInvalidFilter = true);

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || app.Environment.IsProduction())
{
    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger(option =>
    {
        option.RouteTemplate = "Palas/{documentName}/swagger.json";
    });
    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/Palas/v1/swagger.json", "Palas API");
        c.InjectStylesheet("/swagger-ui/theme-material.css");
        c.RoutePrefix = "Palas";
        c.DefaultModelsExpandDepth(-1);
    });
}
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
0 comments No comments
{count} votes