Action Selectors, ActionVerbs in Asp.Net MVC with Example

Action selectors are the attributes that are applied on controller action methods to influence the selection of an action method. If we are writing all the logic in the controller as a single Action Method, that will become a complex to maintain it. 

 

To solve this problem, we need to use Action selectors attributes ([HttpGet], [HttpPost], [HttpPut], [HttpDelete] ) and decorate it with these attributes based on our requirements. We would know which Action Method will execute on which Http Request and it's easy to maintain.

 

  1. [ActionName("Home")] 
  2. [HttpGet] , [HttpPost] , [HttpPut] , [HttpDelete]
  3. [AcceptVerbs(HttpVerbs.Get)] , [AcceptVerbs(HttpVerbs.Delete)]
  4. [AcceptVerbs(HttpVerbs.Post)] ,[AcceptVerbs(HttpVerbs.Put)]

ActionName in Asp.Net MVC

This ActionName attribute is used when you expose an action name with a different name than its method name, or you can use an action name attribute to expose two methods with the same name as the action with different names. The ActionName selector attribute is used to change the name of the action method. The following example shows how to change the name of the action method using the ActionName attribute.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial4.Controllers

{

public class HomeController : Controller

{

[ActionName("Home")]

public ActionResult Index()

{

ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

return View("Index");

}

}

}

Here in above code you can see that we changed name of ActionMethod from Index to Home using ActionName selector. Now you can call this Action method in following way: http://localhost:1111/home/home 

[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in Action Selectors

  • [HttpGet] - It is used to restrict an action method to handle only HTTP GET requests. 
  • [HttpPost] - It is used to restrict an action method to handle only HTTP POST requests. 
  • [HttpPut] - It is used to restrict an action method to handle only HTTP PUT requests. 
  • [HttpDelete] - It is used to restrict an action method to handle only HTTP DELETE requests.

Action Verbs in Asp.Net MVC

The ActionVerbs in MVC control the selection of an action method based on the Http request method like POST, GET, DELETE, etc. For E.g., some times we want the same Action Method to perform on more than one Http Request that time, we can use the Action Verbs attribute that would be like as shown below.

 

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]

public ActionResult Home()

{

return View();

}

The above Home() method will support both get and post requests in MVC. Check the following example to use Action Verbs in controller action methods in MVC.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace Tutorial4.Controllers

{

public class HomeController : Controller

{

 

[AcceptVerbs(HttpVerbs.Get)]

public ActionResult GetPerson()

{

Person p = newPerson ();

return View("Person",p);

}

 

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Insert(Person p)

{

return View("Person");

}

 

[AcceptVerbs(HttpVerbs.Put)]

public ActionResult Update(Person p)

{ 

return View("Person");

}

 

[AcceptVerbs(HttpVerbs.Delete)]

public ActionResult Delete(string ID)

{

return View("Person");

}

}

}