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”; that 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 the 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 the [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.

Visual c sharp

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

Empty

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

Controller

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

MVC five

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

Click on add

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

Home controller

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 the code below.

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

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

Local host

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

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

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

Server 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 the output below.

Current time

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

NonAction

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

As you know, the Controller contains the methods that are public and are termed 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 the [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 the “Time” action method. You can see the code of both action methods below.

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

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 the [ActionName] action selector.

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

Current time

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 the “GetTime” action method. So we need to restrict its access by using the [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. The code of the “GetTimeMethod” method will become as follows.

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

So the output will become.

Server error in application

So as you have seen, the [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 know “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 based on the 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. 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. 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. 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. 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 that are supported by the web server.

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

Conclusion

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


Similar Articles