---
title: "What is the use of the [ProducesResponseType] attribute in a .NET Core API controller method?"  
description: "What is the use of the [ProducesResponseType] attribute in a .NET Core API controller method?"  
author: "Steilla Mitchel"  
published: 2023-08-28  
updated: 2023-08-29  
canonical: https://www.mindstick.com/forum/159704/what-is-the-use-of-the-producesresponsetype-attribute-in-a-dot-net-core-api-controller-method  
category: ".net core"  
tags: ["asp.net core", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# What is the use of the [ProducesResponseType] attribute in a .NET Core API controller method?

What is the use of the [ProducesResponseType] [attribute](https://www.mindstick.com/blog/221/attributes-reflection) in a .NET [Core API](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api) [controller method](https://www.mindstick.com/forum/157312/how-to-call-a-controller-method-after-load-view-successfully-in-mvc-4)?

## Replies

### Reply by Aryan Kumar

The `ProducesResponseType` attribute in a .NET Core [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) [method](https://www.mindstick.com/forum/166/webservice-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 `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#

```plaintext
[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.


---

Original Source: https://www.mindstick.com/forum/159704/what-is-the-use-of-the-producesresponsetype-attribute-in-a-dot-net-core-api-controller-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
