Home/MVC/ActionVerbs Attributes in Asp.Net MVC

ActionVerbs Attributes in Asp.Net MVC

ActionVerbs are used when we want to control the selection of an action method based on a Http request method. For example, if we have two action method with same name then it can be respond based upon the HTTP request type(one action has  to respond to Http Get and another method have respond to Http Post). ASP.NET MVC provides attributes to tweak how the actions can be selected based on the name.

Different types of ActionVerbs

ASP.NET MVC supports different types of ActionVerbs.

  • HttpVerbs.Get – retrieve the details from the server. Parameters will be appended in the query string.
  • HttpVerbs.Post – submit new details.
  • HttpVerbs.Put – Use to update an existing information.
  • HttpVerbs.Delete – To delete an existing information.
  • HttpVerbs.Head – Identical to GET except that server do not return message body
  • HttpVerbs.Patch – Requests that a set of changes described in the request entity be applied to the resource identified by the Request
  • HttpVerbs.Options – Represents a request for information about the communication options available on the request/response identified by the Request

In ASP .NET MVC, it can be achieve in two different ways.

READ  MVC Razor : How to call controller from html radio button click event using Jquery

Method 1: AcceptVerbs Attribute

We can apply multiple http verbs using AcceptVerbs attribute by using OR operator.

public class dotnethelpersController : Controller
{
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult EditEmpDetails(int id)
{
// retrieve the Details
}

Method 2: shortcut attributes

public class dotnethelpersController : Controller
{
[HttpGet]
public ActionResult EditEmpDetails(int id)
{
}

[HttpPost]
public ActionResult EditEmpDetails(string empCode)
{
}

Points to Remember :

  • If we declare the ActionVerbs then the action methods are selected based on the request methods.
  • Using AcceptVerbs attribute, we can use multiple action verbs in a single action method
  • Multiple action methods can have same name by declaring with different action verbs.

One Comment

  1. Bhu August 26, 2016 at 7:45 pm

    Nice article. Very useful information

Leave A Comment