In ASP.NET Core Web APIs, these verbs let you link incoming HTTP requests to certain controller actions. Usually, following RESTful ideas, you should select the HTTP verb that most accurately reflects the operation your API endpoint executes. 

 

GET: Purpose: To get data from a server. It should be a secure process with no server side effects; several identical requests should provide the same outcome. Usually marked with the [HttpGet] element, controller activities that fetch data 

[ApiController]
[Route("api/[controller]")]
public class YourController : ControllerBase
{
   [HttpGet] // Retrieves all products
   public ActionResult<IEnumerable<Product>> GetRecords()   {      
       return Ok(new List<Product>());
   }

   [HttpGet("{id}")] // Retrieves a specific product by ID
   public ActionResult<Product> GetERecord(int id)   {  
       var record= new Product { Id = id, Name = "Example" };
       if (record== null)      {
           return NotFound();
       }
       return Ok(record);
   }
}

 

POST: Often to generate a new resource, purpose is to send data to be processed by the server. [HttpPost] usually decorates controller operations creating new resources in .NET Core APIs. 

public class YourController : ControllerBase {
   [HttpPost]
   public ActionResult<Order> CreateRecrodOrder newOrder)
   {       
       newOrder.Id = 1; // Example of setting an ID after creation
       return CreatedAtAction(nameof(GetOrder), new { id = newOrder.Id }, newOrder);
   }

 


PUT: Used to modify a current resource. The client sends the entire updated representation of the resource. [HttpPut("{id}")]" controller actions updating current resources usually have {id} specified to indicate the resource to be updated. 

public class YourController : ControllerBase{
   [HttpPut("{id}")]
   public IActionResult UpdateCustomer(int id, Customer updatedCustomer)
   {
       if (id != updatedCustomer.Id)  {
           return BadRequest();
       }
       return NoContent(); // Indicates successful update with no response body
   }
}

 

 

PATCH Used to make partial changes to a resource. The client merely transmits the data requiring modification, not the whole resource representation. [HttpPatch("{id}")]" attribute decorates controller. usage that partially update resources. 

public class UsersController : ControllerBase
{
   [HttpPatch("{id}")]
   public IActionResult PartiallyUpdateUser(int id, JsonPatchDocument<User> patchDocument)   {   
       var user = new User { Id = id, Email = "old@example.com" };
       patchDocument.ApplyTo(user, ModelState);

       if (!ModelState.IsValid)  {
           return BadRequest(ModelState);
       }
       // Save Logic
       return NoContent();
   }
}

 

DELETE .NET Core API Usage: Typically, controller operations deleting resources are marked with the [HttpDelete("{id}")] attribute; {id} indicates the resource to be destroyed. 

public class YourController : ControllerBase
{
   [HttpDelete("{id}")]
   public IActionResult DeleteBook(int id)   {
       // Logic to delete the book with the given ID
       // ...
       return NoContent(); // Indicates successful deletion with no response body
   }


Related Question