ASP.NET Core Web API Interview Question #91

How do you add Swagger/OpenAPI documentation to an ASP.NET Core Web API?

Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate

Quick Interview Answer

Register the required OpenAPI/Swagger services during startup and expose the generated document and UI through middleware. The exact package and APIs depend on the application's .NET version and chosen OpenAPI tooling.

Detailed Explanation

Register the required OpenAPI/Swagger services during startup and expose the generated document and UI through middleware. The exact package and APIs depend on the application's .NET version and chosen OpenAPI tooling.

Production exposure should be a deliberate decision. Some organizations publish API documentation publicly, while others restrict interactive documentation for internal APIs.

Code Example

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

app.UseSwagger();
app.UseSwaggerUI();