w3resource

Action verb


In the previous tutorial, we explored action methods and we did a bit of action verbs. In this tutorial, we will take some time and explore action verbs in details.

Action verbs are https labels used to control the selection of action methods in the controller. With action verbs you can achieve a sort of method overloading behavior which is not supported by action methods. For instance, two action methods can be defined with the same name, then using action verbs, one can be made to respond to HTTP GET requests and the other HTTP POST request as illustrated in the following code snippet.

// GET: Activities/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Activities/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create()
        {
            return View();
        }

In most MVC frameworks, there exist support for different Actin verbs. Examples of the most commonly used ones are HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions and HttpPatch.

Any of these attributes can be applied to an action method to indicate the type of request that it will handle. When no action verb is applied to an action method, it defaults to a GET request.

The table below outlines some of the action verbs available in the MVC framework and their uses.

Http method Usage
GET To retrieve the information from the server. Parameters will be appended in the query string.
POST To create a new resource.
PUT To update an existing resource.
HEAD Identical to GET except that server do not return message body.
OPTIONS OPTIONS method represents a request for information about the communication options supported by web server.
DELETE To delete an existing resource.
PATCH To make a full or partial update on the resource.

Having outlined some of these action verbs and their uses, we will not give an illustration on how each of these verbs can be applied to an action method.

public class ActivitiesController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult PostAction()
    {
        return View("Index");
    }



    [HttpPut]
    public ActionResult PutAction()
    {
        return View("Index");
    }

    [HttpDelete]
    public ActionResult DeleteAction()
    {
        return View("Index");
    }

    [HttpHead]
    public ActionResult HeadAction()
    {
        return View("Index");
    }
       
    [HttpOptions]
    public ActionResult OptionsAction()
    {
        return View("Index");
    }
       
    [HttpPatch]
    public ActionResult PatchAction()
    {
        return View("Index");
    }
}

Multiple action verbs can be applied to an action method at once. The GET and POST verbs supports this practice as illustrated in the following example code snippet:

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

So far, we have learnt about action verbs and how they can be applied to action methods, In the next tutorial, we will explore yet another feature of the ASP.NET MVC...Stay hungry



Inviting useful, relevant, well-written and unique guest posts