HTTP Verbs in ASP.NET Web API
HTTP Verbs in ASP.NET Web API
The HTTP Verbs are used to handle different type of Http requests. The MVC framework includes HttpGet, HttpPost, HttpPut, HttpDelete, HttpHead, HttpOptions and HttpPatch HTTP Verbs. They are also known as Action Verbs.
We can apply one or more action verbs to an action method to handle different HTTP requests. If we do not apply any action verbs to an action method, then it will handle HttpGet request by default.
Let us see all of them, one by one –
- GET – It is used to retrieve the information from the server. Parameters are appended in the query string.
2. POST – It is used to create a new resource.
3. PUT – It is used to update an existing resource.
4. HEAD – It is identical to GET except that server do not return the message body.
5. OPTIONS – It represents a request for information about the communication options supported by the web server.
6. DELETE – It is used to delete an existing resource.
7. PATCH – It is used to update the resource completely or partially.
Example -
public class HomeController : Controller
{
[HttpGet]
public ActionResult GetAction() // handles GET requests by default
{
return View();
}
[HttpPost]
public ActionResult PostAction() // handles POST requests by default
{
return View("Index");
}
[HttpPut]
public ActionResult PutAction() // handles PUT requests by default
{
return View("Index");
}
[HttpDelete]
public ActionResult DeleteAction() // handles DELETE requests by default
{
return View("Index");
}
[HttpHead]
public ActionResult HeadAction() // handles HEAD requests by default
{
return View("Index");
}
[HttpOptions]
public ActionResult OptionsAction() // handles OPTION requests by default
{
return View("Index");
}
[HttpPatch]
public ActionResult PatchAction() // handles PATCH requests by default
{
return View("Index");
}
}
