ASP.NET Core › API Documentation with Swagger/OpenAPI

How do you add XML documentation comments to enrich generated Swagger documentation with descriptions?

Intermediate

Answer

Enabling GenerateDocumentationFile in the .csproj causes the compiler to extract /// XML doc comments into a .xml file, which Swashbuckle can be configured to read via IncludeXmlComments(), populating endpoint summaries, parameter descriptions, and response descriptions in the generated OpenAPI document directly from your existing code comments.
<PropertyGroup>
  <GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

/// <summary>Gets a product by its ID.</summary>
/// <param name="id">The product's unique identifier.</param>
[HttpGet("{id}")]
public IActionResult GetProduct(int id) { ... }

builder.Services.AddSwaggerGen(c => {
    c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "MyApi.xml"));
});
Real-world example A team writes XML doc comments primarily for IntelliSense during development, then discovers those same comments automatically populate meaningful endpoint descriptions in Swagger UI, giving API consumers documentation for free without duplicated effort.

Common follow-ups: What happens if XML comments are missing for some endpoints?;How do you document response types beyond just the success case?