Learn About Action Selectors In ASP.NET MVC

About This Article

This article will tell you almost everything about action selectors used in ASP.NET MVC action methods. I am writing this article to tell you the basic to advanced foremost concepts about action selectors.

The topics to be covered are,

  1. What are action methods?
  2. What are the action selectors?
  3. Types of action selectors

    1. NonAction
    2. ActionName

  4. ActionVerbs and its types

Introduction and Background

Action selectors are the attributes that can be applied to action methods to influence the selection of the action method. Action selectors decide which action methods get invoked in response to an HTTP request. You have seen in the  “Pipeline in MVC” section of my previous article “MVC Architecture and its Pipeline”; the HTTP request comes to “Routing”; then goes to “Controller Initialization”; then “Action Execution”. Then only the “Result Execution” comes into action to render the View. In the portion of “Action Execution,” action selectors come into action and play an important role in selecting the correct action method to handle a particular HTTP request. Let’s take a look at action methods.

What are the Action Methods?

Action methods are the methods in the controller whose name should be matched with the method name in the request URL. These methods then execute the request and then create the response. There can be many action methods in a controller. Also, you can set the default action method for a controller to execute the request.

What are the Action Selectors?

When a request comes to routing engine, a specific action method must be selected in the response to that request. So this selection of action method is the responsibility of action selectors. Action selector does fulfill its responsibility due to the attribute applied on the top of the action method. Let’s look at the types of action selectors.

Types of Action Selectors

There are three types of action selectors,

  1. NonAction
  2. ActionName
  3. ActionVerbs

ActionName

[ActionName] attribute is an action selector which is used for a different name of the action method. We use [ActionName] attribute when we want that action method to be called with a different name instead of the actual name of the method. You can see the example below, in which there is an action method with the original name of “Time”.

Let’s create a project named ActionSelectorsDemo. Select “Visual C#” then select “ASP.NET Web Application” then give a name to the project and click OK.

ActionName

Select “Empty” and mark “MVC” then click OK, as shown below.

MVC

The project is created successfully. Now, create a Controller as shown below.

create a controller

Select the type of Controller as “MVC 5 Controller – Empty” and click “Add”.

MVC 5 Controller

Then, give the name of the Controller and click “Add”.

Controller

Make the following changes in Index action method of the HomeController, as shown below in the screenshot.

HomeController

Now, the project is ready to take a look at a practical example of [ActionName] action selector. Now, let’s add an action method with the name of “Time” whose purpose is to print the current time. See code below,

  1. public String Time() {  
  2.     return @ "Current Time is: " + DateTime.Now;  
  3. }  

When you build and run the application with this /Home/Time URL, you should see the following output,

Home/Time URL

Now we are going to use [ActionName] attribute with the “Time” action method. See code below,

  1. [ActionName("GetTime")]  
  2. public String Time() {  
  3.     return @ "Current Time is: " + DateTime.Now;  
  4. }  

Now build and run the application with this /Home/Time URL, you should see the following error, it is because of [ActionName] action selector.

Error

By using this action selector, you have changed the name of the action method, now the URL /Home/GetTime only works instead of /Home/Time. See output below,

ActionName

Hence you have seen that [ActionName] action selector can change the name of the action method.

NonAction

[NonAction] attribute is an action selector which indicates a public method of the controller is not an action method. We use [NonAction] attribute when we want that a method shouldn’t be treated as an action method.

As you know, Controller contains the methods which are public and are termed as action methods because of one to one mapping with the HTTP requested URL. But when you want that, any method of Controller shouldn’t be treated as an action method, then you have to use [NonAction] attribute. 

 Example

Now let’s make some changes in the HomeController of ActionSelectorsDemo project. Add another method named “GetTimeMethod” whose responsibility is also to print the current time. And then call this newly added method into “Time” action method. You can see code of both action methods below,

  1. [ActionName("GetTime")]  
  2. public String Time() {  
  3.     return GetTimeMethod();  
  4. }  
  5. public String GetTimeMethod() {  
  6.     return @ "Current Time is: " + DateTime.Now;  
  7. }  

Now observe the output, after building and running the application. When we run the application with this /Home/Time URL, we’ll see an error because we are bound to use the name “GetTime” for this action method because of [ActionName] action selector.

Now when you run the application with this /Home/GetTimeMethod, you should see the output given below,

GetTimeMethod

As you have seen, you are still able to access GetTimeMethod while there is no need to access it because it is producing the same output as produced by “GetTime” action method. So we need to restrict its access by using [NonAction] action selector because it makes the method a non-action method. So it can’t be called in the request URL at all. Let’s see its example.

Code of “GetTimeMethod” method will become as follows,

  1. [NonAction]  
  2. public String GetTimeMethod() {  
  3.     return @ "Current Time is: " + DateTime.Now;  
  4. }  

So output will become,

error

So as you have seen, [NonAction] action selector is used to make an action method Non-Action method.

ActionVerbs

You have seen in the beginning of this article what actions are.  And you should also have knowledge about “Verbs” which are defined as “words which are used to describe an action, state or occurrence of an event”. So as the name suggests, “ActionVerbs” can be defined as “the words that describe the action, purpose, and state of the action method of a Controller”. Now, let’s move towards its technical explanation.

When an HTTP request comes to the controller on a specific action method, then this selector helps us to select the correct action method on the basis of HTTP requested method. It also helps us to create more than one action method with the same name, also referred to as overloading. So to differentiate these action methods, we use action verbs. We’ll see its example later in this article.

MVC framework supports different ActionVerbs, shown as follows,

  • HttpGet
    It is used to get information from the server. And when this applies to the action method, it restricts the action method to accept only HTTP GET requests.

  • HttpPost
    It is used to post information on the server. And when this applies to the action method, it restricts the action method to accept only HTTP POST requests.

  • HttpPut
    It is used to create or replace existing information on the server, but can’t update. And when this applies to the action method, it restricts the action method to accept only HTTP PUT requests.

  • HttpDelete
    It is used to delete any existing information from the server. And when this applies to an action method, it restricts the action method to accept only HTTP Delete requests.

  • HttpPatch
    It is used to fully or partially update any existing information on the server. And when this applies to the action method, it restricts the action method to accept only HTTP Patch requests.

  • HttpOptions
    It is used to represent information about the options of the communication whose are supported by the web server.

Action verbs are very popular in using APIs. In MVC framework, if you don’t apply any attribute of action selector on the top of action method, when the method is called from the HTTP request, then that HTTP request will behave like HttpGet request by default.

Conclusion

I hope this article has helped you in understanding all the concepts about action selectors in ASP.NET MVC. And stay tuned for my next article. If you have any query then feel free to contact me in the comments section. Also, give feedback, either positive or negative, it will help me to make my articles better and increase my enthusiasm to share my knowledge.