Action Selectors, ActionVerbs in Asp.Net MVC with Example

  Total Views : 4164
  Comments : 1
 
  Keywords : Asp.net mvc action selectors, Attribute types in asp.net mvc action selectors, Actionverbs in asp.net mvc with example,

Action selectors are the attributes which are applied on controller action methods to influence the selection of an action method. If we are writing all the logic in controller as a single Action Method then 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 then we would know which Action Method will execute on which Http Request and its 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 action method. Following example shows how to change the name of action method using 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 is used to control the selection of an action method based on Http request method like POST, GET, DELETE, etc. E.g. Some times we want same Action Method to perform on more than one Http Request that time we can use Action Verbs attribute that would be like as shown below 

 

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

public ActionResult Home()

{

return View();

}

Above Home() method will support both get and post requests in mvc. Check 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");

}

}

}

 Comments (1)

  • charul tutlane registered profile image
     
    charul   Reply

    please explain each with output

     Oct 06, 2016

Leave a comment

  • Default Tutlane User