ActionVerbs in ASP.NET MVC with Example

108
ActionVerbs in mvc

ActionVerbs in MVC

In the previous article, we learned about asp.net mvc action selectors and action methods. In this article, we will learn about the ActionVerbs selectors attribute in MVC.

The ActionVerbs selector is used when you want to control the selection of an action method based on an Http request method. For example, you can define two different action methods with the same name but one action method responds to an HTTP Get request and another action method responds to an HTTP Post request.

MVC framework supports different ActionVerbs, such as HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions & HttpPatch. You can apply these attributes to the action method to indicate the kind of Http request the action method supports. If you do not apply any attribute then it considers it a GET request by default.

The following figure illustrates the HttpGET and HttpPOST action verbs.

 action verbs in mvc

The following table lists the usage of HTTP methods:

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 does not return a message body.
OPTIONS The OPTIONS method 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 of the resource.

Visit W3.org for more information on Http Methods.

The following example shows different action methods supports different ActionVerbs:

Action verbs examples in MVC

You can also apply multiple HTTP verbs using the AcceptVerbs attribute. GetAndPostAction method supports both, GET and POST ActionVerbs in the following example:

Example 2:

LEAVE A REPLY

Please enter your comment!
Please enter your name here