HttpGet, HttpPost, HttpPut
The ActionVerbs selector is used to deal with various Http requests.
HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions, and HttpPatch are
action verbs in the MVC framework. To handle different HTTP requests, you
can add one or more action verbs to an action form. If you don't use any
action verbs in an action form, it will default to handling HttpGet
requests.
The following table shows how HTTP methods are used:
| Http method | Usage |
|---|---|
| GET | To obtain details from the server. The query string will be appended with parameters. |
| POST | To create a new resource. |
| PUT | To update an existing resource. |
| HEAD | Identical to GET except that server do not return the message body. |
| OPTIONS | It represents a request for information about the communication options supported by the web server. |
| DELETE | To delete an existing resource. |
| PATCH | To full or partial update the resource. |
The following example illustrates how to use ActionVerbs in the
Controller to manage various forms of HTTP requests:
public class StudentController : Controller
{
public ActionResult Index() // 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");
}
}
The AcceptVerbs attribute can also be used to add multiple action verbs,
as shown below.
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult GetAndPostAction()
{
return RedirectToAction("Index");
}
0 Comments