Introduction:

In this article we will learn what is Action Verbs selector or how to use Action Verbs selector or why use Action Verbs selector or how to use GET and POST in mvc with example.

Description: 

The Action Verbs selector used to control the selection of an action method based on the HTTP request method GET and POST. You can define two different action methods with the same name in a controller,

but one action called to HTTP Get request and another action method called with an HTTP Post request.


The following figure HttpGET and Http POST action verbs.


Action Verbs in Asp.Net MVC with Example

  The following lists the usage of http methods:


Http method

Usage

GET

To retrieve the data from the server. Parameters will be appended in the query string.

POST

Send resource to the server To create a new resource.

PUT

Send resource to the server  To update an existing resource.

HEAD

This is identical to a GET request, but only returns the headers for the response, not the response body

OPTIONS

OPTIONS method represents a request for information about the communication options supported by web server.

DELETE

To delete an existing resource.

PATCH

Submits a partial modification to a resource. If you only need to update one field for the resource, you may want to use the PATCH method.

 

The following example shows action methods supports different ActionVerbs:

 

        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
 
        [HttpPost]
        public ActionResult Post()
        {
            return View("Index");
        }
 
 
        [HttpPut]
        public ActionResult Put()
        {
            return View("Index");
        }
 
        [HttpDelete]
        public ActionResult Delete()
        {
            return View("Index");
        }
 
        [HttpHead]
        public ActionResult Head()
        {
            return View("Index");
        }
 
        [HttpOptions]
        public ActionResult Options()
        {
            return View("Index");
        }
 
        [HttpPatch]
        public ActionResult Patch()
        {
            return View("Index");
        }

 

You can also use multiple http verbs on an action by using AcceptVerbs attribute. Put And Post method Select both, Put and POST On HTTP Request. In the following example:

 

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        public ActionResult PutAndPost()
        {
            return RedirectToAction("Index");
        }

 

I hope it will help to you.



0 comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like