ASP.NET Core Web API Interview Question #92
How can ProducesResponseType improve API documentation?
Routing, Versioning, OpenAPI/Swagger & API Documentation Mid-Level Intermediate
Quick Interview Answer
ProducesResponseType declares expected HTTP response codes and, optionally, response body types for an action. OpenAPI tooling can use this metadata to describe success and failure responses more accurately.
Detailed Explanation
ProducesResponseType declares expected HTTP response codes and, optionally, response body types for an action. OpenAPI tooling can use this metadata to describe success and failure responses more accurately.
It is especially helpful when an action can return several meaningful outcomes that are not obvious from its C# return type alone.
Code Example
[HttpGet("{id:int}")]
[ProducesResponseType<ProductResponse>(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<ProductResponse>> Get(int id)
{
// ...
return Ok();
}