The ProducesResponseType attribute in a .NET Core APIcontrollermethod is used to indicate the types and HTTP status codes that can be returned by the method. This attribute is used by tools like Swagger to generate more descriptive response details for web API help pages.
The ProducesResponseType attribute has two properties:
Type: The type of the object that can be returned by the method.
StatusCode: The HTTP status code that will be returned if the method returns the specified type.
The following code shows how to use the ProducesResponseType attribute:
C#
[ApiController]
[Route("[controller]")]
public class ProductController : ControllerBase {
[HttpGet]
[ProducesResponseType(typeof(Product), StatusCodes.Status200OK)]
public IActionResult Get() {
// Get the product data from the database.
// ...
// Return the product data.
return Ok(product);
}
}
In this code, the Get() action method returns a Product object if the product data is found in the database. The
ProducesResponseType attribute indicates that the method can return a
Product object and that the HTTP status code will be 200 OK if the product data is found.
The ProducesResponseType attribute is optional, but it is a good practice to use it to help users understand the possible responses from your API.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
ProducesResponseTypeattribute in a .NET Core API controller method is used to indicate the types and HTTP status codes that can be returned by the method. This attribute is used by tools like Swagger to generate more descriptive response details for web API help pages.The
ProducesResponseTypeattribute has two properties:Type: The type of the object that can be returned by the method.StatusCode: The HTTP status code that will be returned if the method returns the specified type.The following code shows how to use the
ProducesResponseTypeattribute:C#
In this code, the
Get()action method returns aProductobject if the product data is found in the database. TheProducesResponseTypeattribute indicates that the method can return aProductobject and that the HTTP status code will be 200 OK if the product data is found.The
ProducesResponseTypeattribute is optional, but it is a good practice to use it to help users understand the possible responses from your API.