HTTP verbs are used to perform different actions on resources. These HTTP methods define the type of operation that should be performed on a resource when making a request to a web API endpoint.
Here are some commonly used HTTP verbs or methods in ASP.NET Web API with examples:
HTTP Method | CRUD | Collection Resource (e.g. /products) | Single Resouce (e.g. /products/123) |
---|---|---|---|
POST | Create | 201 (Created), ‘Location’ header with link to /products/{id} containing new ID | Avoid using POST on a single resource |
GET | Read | 200 (OK), list of users. Use pagination, sorting, and filtering to navigate big lists | 200 (OK), single user. 404 (Not Found), if ID not found or invalid |
PUT | Update/Replace | 405 (Method not allowed), unless you want to update every resource in the entire collection of resource | 200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid |
PATCH | Partial Update/Modify | 405 (Method not allowed), unless you want to modify the collection itself | 200 (OK) or 204 (No Content). Use 404 (Not Found), if ID is not found or invalid |
DELETE | Delete | 405 (Method not allowed), unless you want to delete the whole collection — use with caution | 200 (OK). 404 (Not Found), if ID not found or invalid |
1. GET
Get is used to retrieves a representation of a resource or a collection of resources. It should be used for safe and idempotent operations that do not modify the state of the server.
Example - Retrieve a list of products.
[HttpGet]
public IHttpActionResult GetProducts()
{
// Retrieve and return the list of products
}
2. POST
Post is used to send data to the server to create a new resource. It is often used for submitting form data or sending data to be processed on the server.
Example - Create a new product.
[HttpPost]
public IHttpActionResult CreateProduct(Product product)
{
// Create the product and return the result
}
3. PUT
Put is used to update an existing resource or create a new resource with a specific identifier. It is used to updates an existing resource with the provided data. It replaces the entire resource with the new representation.
Example - Update an existing product.
[HttpPut]
public IHttpActionResult UpdateProduct(int id, Product product)
{
// Update the product with the specified ID and return the result
}
4. DELETE
Delete verb is used to delete an existing resource.
Example - Delete a product.
[HttpDelete]
public IHttpActionResult DeleteProduct(int id)
{
// Delete the product with the specified ID and return the result
}
5. PATCH
Partially updates an existing resource. It allows modifying only specific fields or properties of a resource without replacing the entire representation.
Example - Partial update of a product.
[HttpPatch]
public IHttpActionResult PartialUpdateProduct(int id, Delta<Product> productDelta)
{
// Apply the partial update to the product with the specified ID and return the result
}
6. OPTIONS
It is used to retrieves the HTTP methods and other options available for a resource. It is often used to retrieve information about the API and its capabilities.
Example - Get the allowed methods for a resource.
[HttpOptions]
public IHttpActionResult GetOptions()
{
// Return the allowed methods and other information about the resource
}
7. HEAD
This is identical to a GET request, but only returns the headers for the response, not the response body. Theoretically faster, commonly used for checking to see if a particular resources exists or can be accessed.
Here's an example of a HEAD request.
HEAD /api/products/123 HTTP/1.1
Host: tutorialstrend.com
The server would respond with the headers for the requested resource, without including the actual content.
These are some of the most commonly used HTTP verbs in ASP.NET Web API. By using the appropriate HTTP verb for each API endpoint, you can follow the RESTful principles and provide a clear and consistent interface for interacting with your API.