About the 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,
- What are action methods?
- What are the action selectors?
- Types of action selectors
- NonAction
- ActionName
- ActionVerbs and its types
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,
- NonAction
- ActionName
- ActionVerbs
[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.
- public String Time() {
- return @ "Current Time is: " + DateTime.Now;
- }
- [ActionName("GetTime")]
- public String Time() {
- return @ "Current Time is: " + DateTime.Now;
- }
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.
- [ActionName("GetTime")]
- public String Time() {
- return GetTimeMethod();
- }
- public String GetTimeMethod() {
- return @ "Current Time is: " + DateTime.Now;
- }
Now when you run the application with this /Home/GetTimeMethod, you should see the output given below,
Code of “GetTimeMethod” method will become as follows,
- [NonAction]
- public String GetTimeMethod() {
- return @ "Current Time is: " + DateTime.Now;
- }
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.
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.
No comments:
Post a Comment