ASP.NET Core Web API Interview Question #94

What are XML documentation comments used for in API documentation?

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

Quick Interview Answer

XML documentation comments can describe controllers, endpoints, parameters, return values, and DTO properties in source code. OpenAPI tooling can include the generated XML file to enrich API documentation.

Detailed Explanation

XML documentation comments can describe controllers, endpoints, parameters, return values, and DTO properties in source code. OpenAPI tooling can include the generated XML file to enrich API documentation.

Comments should explain the public contract and important behavior rather than simply restating method names.

Code Example

/// <summary>
/// Returns a product by its identifier.
/// </summary>
/// <param name="id">Product identifier.</param>
/// <response code="200">Product found.</response>
/// <response code="404">Product not found.</response>
[HttpGet("{id:int}")]
public IActionResult Get(int id) => Ok();