Action Selector in MVC

Action selector is the attribute that can be applied to the action methods. It helps the routing engine to select the correct action method to handle a particular request. In this article, I am going to discuss the ideas about Action Selector in MVC. Please read our previous articles before moving on to this article where we discussed Action Method in ASP.NET MVC.

Action Selector

Action Selectors are the attributes that can be applied to the action methods in MVC application.
Those are helping the routing engine to select the appropriate action method from MVC controller to handle the specific request.
There are three types of attributes.

  • ActionName
  • NonAction
  • ActionVerbs

Action Name

ActionName attribute allows us to specify a different name than the actual name of a method.
It means that you can request the URL based on the action name rather than the action method name.

1[ActionName("FindEmployeeDetails")]
2public ActionResult GetID(string id)
3{
4    return Content("Your ID is : " + id);
5}

As per the above example, We applied the actionname attribute on the GetID method. Now It will be invoked on http://localhost/Employee/FindEmployeeDetails/90 request instead of http://localhost/Employee/GetID/90.
If you request http://localhost/Employee/GetID/90 address then the application throws 404 error on your browser.

NonAction

NonAction selector is an attribute and can apply to the public method, which indicates that a method of a Controller is not an action method.
You can able to create your own public methods in MVC controller with the help of NonAction select.
Please follow the below example.

1[NonAction]
2public ActionResult GetName(string Name)
3{
4    return Content("Your Name is : " + Name);
5}

If you request http://localhost/Employee/GetName/King address on your browser then the application throws 404 error on your browser.

ActionVerbs:

Action Verbs selector is usually used when you want to manipulate the action method based on the HTTP request.
In MVC application, You can create more than two different action methods with the same name with the help of action verbs. One method responds to HTTP Get request method and another action method responds to HTTP Post request method.
By default action method or without action verbs action method acts as responds HTTP Get the request method.
MVC framework supports different ActionVerbs such as HttpGetHttpPostHttpPutHttpDelete etc.

Please follow the below example.

1[HttpGet] //Action Verbs for HTTP Get Reuqest
2public ActionResult Edit(string id)
3{
4    //Retrieve Data from Database
5    return View();
6}
7[HttpPost] //Action Verbs for HTTP Post Reuqest
8public ActionResult Edit(employee Emp)
9{
10    //Update new employee details in Database
11    return View();
12}

As per the above example, you have two different action names with the same name. The first method “Edit(string id)” used Action Verbs for HTTP Get Request. The second method “Edit(employee Emp)” used Action Verbs for HTTP Post Request.

Please follow the below table for details about ActionVerbs

Http methodUsage
GETTo retrieve the information from the server.
POSTTo post information on Server.
PUTTo update an existing resource.
HEADIdentical to GET except that server do not return a message body.
OPTIONSIt represents a request for information about the communication options supported by the web Server.
DELETETo delete an existing resource.
PATCHTo full or partial update the resource.
HTTP Request Types

Warm UP – Action Selector in MVC

  • ActionSelectors are the attributes that can be applied to the action methods in MVC application.
  • Action selectors attributes are mainly three types such as ActionNameNonAction and ActionVerbs.
  • ActionName attribute allows us to specify a different name than the actual name of method.
  • NonAction selector is an attribute and can apply on public method, which indicates that a method of a Controller is not an action method.
  • ActionVerbs selector is usually used when you want to manipulate the action method based on HTTP request.

Please read more about Action Selector in MVC from Microsoft

Leave a Comment

Your email address will not be published. Required fields are marked *