---
title: "Explain the use of HttpPost and HttpPut attributes in the API Controller."  
description: "Explain the use of HttpPost and HttpPut attributes in the API Controller."  
author: "Utpal Vishwas"  
published: 2023-10-29  
updated: 2023-10-30  
canonical: https://www.mindstick.com/forum/160298/explain-the-use-of-httppost-and-httpput-attributes-in-the-api-controller  
category: ".net core"  
tags: ["http post", ".net core", ".net core api"]  
reading_time: 3 minutes  

---

# Explain the use of HttpPost and HttpPut attributes in the API Controller.

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the use of HttpPost and [HttpPut](https://www.mindstick.com/forum/160302/what-is-the-importance-of-request-headers-in-httpput-and-httpdelete-operations) [attributes](https://www.mindstick.com/articles/13105/2-attributes-that-make-your-odoo-ecommerce-theme-productive-and-engaging) in the [API Controller](https://www.mindstick.com/forum/159704/what-is-the-use-of-the-producesresponsetype-attribute-in-a-dot-net-core-api-controller-method).

## Replies

### Reply by Aryan Kumar

The **[HttpPost]** and **[HttpPut]** attributes in an [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) controller are used to specify which HTTP methods (verbs) a particular action method should respond to. These attributes play a significant role in determining the behavior of the API and how it handles incoming requests. Here's an explanation of their use:

## [HttpPost] Attribute:

**HTTP POST Request Handling**: The **[HttpPost]** attribute is used to decorate action methods that are responsible for handling HTTP POST requests. HTTP POST is typically used for creating new resources on the server or for making non-idempotent changes to existing resources.

**Resource Creation**: In the context of an API, action methods marked with **[HttpPost]** are often used to create new resources. For example, you might use it to create a new user account, add a product to a catalog, or submit a comment on a blog post.

**Request Body**: POST requests usually include a request body, which contains the data necessary to create or update a resource. This data can be in various formats, such as JSON, XML, or form data, and the action method is responsible for parsing and processing this data.

**Data Submission**: The **[HttpPost]** attribute is commonly associated with forms or data submission where the client sends data to the server, and the action method processes that data to create a new resource.

## [HttpPut] Attribute:

**HTTP PUT Request Handling**: The **[HttpPut]** attribute is used to decorate action methods that handle HTTP PUT requests. HTTP PUT is typically used to update or replace an existing resource identified by a URI.

**Resource Update or Replacement**: In an API, action methods marked with **[HttpPut]** are used to update or replace an existing resource with the data provided in the request. The client sends the entire updated representation of the resource.

**Idempotent**: HTTP PUT requests are idempotent, meaning that making the same request multiple times will have the same effect as making it once. This is important for safe and predictable updates.

**Resource Identification**: In a RESTful API, the URI identifies the resource to update, and the data in the request body contains the new representation of the resource.

**Resource Replacement**: A PUT request can replace the entire resource at the specified URI, making it useful for operations that require the full representation of a resource to be updated.

```plaintext
[ApiController]
[Route("api/resources")]
public class ResourcesController : ControllerBase
{
    [HttpPost]
    public IActionResult CreateResource([FromBody] ResourceModel model)
    {
        // Logic to create a new resource using data from the model
        // ...
        return CreatedAtRoute("GetResource", new { id = createdResourceId }, createdResource);
    }

    [HttpPut("{id}")]
    public IActionResult UpdateResource(int id, [FromBody] UpdatedResourceModel model)
    {
        // Logic to update or replace the resource identified by 'id' with data from the model
        // ...
        return NoContent(); // Return a 204 No Content status code for a successful update.
    }
}
```


---

Original Source: https://www.mindstick.com/forum/160298/explain-the-use-of-httppost-and-httpput-attributes-in-the-api-controller

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
