Explain to me about Action Selector in asp.net MVC with suitable example.
What is Action selector in MVC with example?
35604-Feb-2020
Updated on 04-Feb-2020
Home / DeveloperSection / Forums / What is Action selector in MVC with example?
Explain to me about Action Selector in asp.net MVC with suitable example.
Nishi Tiwari
04-Feb-2020Action selectors are defined as the attributes that are applied to controller action methods to influence the selection of an action method. So, if we are writing all the logic in the controller as a single Action Method then that will become a complex for us to maintain it.
Now 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 also it is 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
The ActionName attribute is used when we expose an action name with a different name than its method name, or we can use an action name attribute to expose the two different methods with the same name as the action with different names. This ActionName selector attribute is used to change the name of the action method. Following given example shows how to change the name of action method using ActionName attribute
In the above code, we can see that we changed the name of ActionMethod from Index to Home using ActionName selector. Now we can call this Action method in the following way: http://localhost:1111/home/home
[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in Action Selectors