ASP.NET Web API is built around the principles of REST, which utilizes standard HTTP methods to perform operations on resources. Each HTTP method corresponds to a specific action that can be performed on a resource. Below are the most commonly used HTTP methods in ASP.NET Web API, along with explanations and sample code.

1. GET

The GET method is used to retrieve data from the server. It is a safe and idempotent method, meaning it does not modify any resources and can be called multiple times without changing the result.

        
public class ProductsController : ApiController
{
// GET api/products
public IEnumerable<Product> Get()
{
return GetProducts(); // Fetch all products
}

// GET api/products/1
public IHttpActionResult Get(int id)
{
var product = GetProductById(id); // Fetch product by ID
if (product == null)
{
return NotFound(); // Return 404 if not found
}
return Ok(product); // Return product as JSON
}
}

2. POST

The POST method is used to create a new resource on the server. It is not idempotent, meaning that calling it multiple times can result in different outcomes (e.g., creating multiple resources).

        
// POST api/products
public IHttpActionResult Post([FromBody] Product product)
{
if (product == null)
{
return BadRequest("Invalid data.");
}
SaveProduct(product); // Save product to database
return CreatedAtRoute("DefaultApi", new { id = product.Id }, product); // Return 201
}

3. PUT

The PUT method is used to update an existing resource on the server. It is idempotent, meaning that calling it multiple times with the same data will not change the result after the first call.

        
// PUT api/products/1
public IHttpActionResult Put(int id, [FromBody] Product product)
{
if (product == null || product.Id != id)
{
return BadRequest("Invalid data.");
}
UpdateProduct(product); // Update product in database
return StatusCode(HttpStatusCode.NoContent); // Return 204
}

4. DELETE

The DELETE method is used to remove a resource from the server. It is idempotent, meaning that calling it multiple times will have the same effect as calling it once (the resource will be deleted).

        
// DELETE api/products/1
public IHttpActionResult Delete(int id)
{
DeleteProduct(id); // Delete product from database
return StatusCode(HttpStatusCode.NoContent); // Return 204
}

5. PATCH

The PATCH method is used to apply partial modifications to a resource. It is not idempotent, as applying the same patch multiple times may yield different results.

        
// PATCH api/products/1
public IHttpActionResult Patch(int id, [FromBody] JsonPatchDocument<Product> patchDoc)
{
var product = GetProductById(id); // Fetch product by ID
if (product == null)
{
return NotFound(); // Return 404 if not found
}
patchDoc.ApplyTo(product); // Apply the patch
UpdateProduct(product); // Update product in database
return Ok(product); // Return updated product
}

Conclusion

ASP.NET Web API leverages standard HTTP methods to perform operations on resources, adhering to RESTful principles. Understanding these methods—GET, POST, PUT, DELETE, and PATCH—is essential for building effective and efficient web services. By using these methods appropriately, developers can create APIs that are intuitive and easy to use for clients.