Please explain to me what is Action Selector in Asp.Net MVC with an example.
Define Action Selector in MVC.
53830-Jan-2020
Updated on 30-Jan-2020
Home / DeveloperSection / Forums / Define Action Selector in MVC.
Please explain to me what is Action Selector in Asp.Net MVC with an example.
Nishi Tiwari
30-Jan-2020ASP.NET MVC Action Selectors
Action selectors are the attributes that are applied to the action methods of a controller. It is used for selecting the correct action method to call as per the request given. MVC gives the two action selector attributes:
1. ActionName
2. Action Verbs
ActionName
This attribute will allow us to specify a different name for the action method. It is most useful when we want to call action by a different name.
For Example
We are using the ActionName attribute to give different names for the index action method. The controller code will look like this:
// MusicStoreController.cs
Now, we need to create a view in the MusicStore folder with the same name as the ActionName. So, we have created a store.cshtml file which has the following code.
// store.cshtml
Output:
The following output will be produced when action will be called with a different name that is"store".
ActionVerbs
ASP.NET MVC also provides action verbs that are applied to the action methods and works for HttpRequest methods. There are various ActionVerbs which are listed below.
o HttpPost
o HttpGet
o HttpPut
o HttpDelete
o HttpOptions
o HttpPatch
ActionVerbs are the name of the HTTP requests which a controller will handle. We can use it for selecting the action methods.
Example
In this example, we are trying to access an index action by get request, which will be accessible only for HTTP post requests. The controller code may look like this:
// MusicStoreController.cs
The Index file for MusicStoreController.
// index.cshtml
Output:
It will produce the following output when the index action will be called.
It will produce the error message when we make a get a request for the store action method.