Hello friends, I hope you all are having fun with your lives. In today’s tutorial, we are gonna have a look at Action Selectors in ASP.NET MVC. It’s the 11th tutorial in ASP.NET MVC series and if you have covered so far then that means you really want to learn this language. Action Selectors are also new for you as we haven’t discussed them yet but they are not that difficult to understand.
Action Selectors are built-in attributes of ASP.NET which are directly applied to action methods of Controllers. We use Action Selectors to influence on Controller’s action methods. In simple words, we use Action Selectors to guide URL Routers in selection of correct action method. Let’s have a detailed overview of Action Selectors:
Action Selectors in ASP.NET MVC
- Action Selectors are built-in attributes provided in ASP.NET MVC and are directly influence our Controller’s action methods.
- Action Selectors guide the URL Routers to respective action method of Controller which then opens up View for the user.
- Action Selectors are simple C# classes.
- There are 3 types of Action Selectors available in ASP.NET MVC, named as:
- So, let’s discuss each of them in detail:
1. ActionName Selector in ASP.NET MVC
- ActionName Selector (as the name suggests) is used to change the name of Action from the name of Controller’s Method.
- Let’s open the StudentController.cs file which we created in Tut # 06: Create a New Controller in ASP.NET MVC.
- I have added an action Method here Get() and above this method I have placed [ActionName(“GetAction”)], as shown in below figure:
- If we want to access this action method Get(), then we have to place localhost:12345/Student/Get in our browser.
- But after placing the ActionName Selector, now we have to use localhost:12345/Student/GetAction in order to call Get() Method.
- So, in this case the name of action is GetAction but the name of method is Get.
- But if we don’t use ActionName Selector, then name of both Action and Method is Get.
- Let’s have a look at what’s the effect of ActionName Selector:
- As you can see in above figure that when we have used Get then we received Server Error.
- When we have used GetAction (assigned in ActionName) then our Method Get() is called.
- Here’s the video demonstration of ActionName Selector in detail:
2. NonAction Selector in ASP.NET MVC
- NonAction Selector is another built-in attribute of ASP.NET MVC and as the name name suggests, it is used to remove the Action from action method.
- If you place NonAction Selector before an action method in your Controller then you won’t be able to access that method directly from its action.
- URL Router will think this method doesn’t exist, if its called directly.
- Let’s add some code in our StudentController.cs file, as shown in below figure:
- I have added [NonAction] before our action method Get1(), that’s how we specify nonAction Selector in ASP.NET MVC.
- If you have a look in the Index Method then you can see I have called Get1 there.
- Now when we open the Home Page of our site, then this Get1 action method will be called and it will work fine.
- But if we want to call the Get1 action method directly then it won’t open up and will give us a Server error.
- That’s because we have placed nonAction Selector before it.
- Results of both Home page & direct calling the action method are shown in below figure:
- So, its cleared from the above figure that nonAction has removed the action from our method and now its just a simple public method, which we can call from inside but no one can access it from outside directly.
- Here’s the video demonstration of nonAction Selector, must watch it for live action:
3. ActionVerbs Selector in ASP.NET MVC
- ActionVerbs Selector is third built-in selector in ASP.NET MVC and is used to assign some specified HTTP request to action methods.
- It helps the URL router in selection of correct action method.
- Let’s say you have two action methods in your project and they have a same name but you want to use one for GETting data and the second one for POSTing data.
- Let’s have a look at the similar situation in below code:
- AS you can see in above figure that we have written two action methods of the same name Get1.
- So, now if you call it in your browser then you will get Server Error, as shown in below figure:
- So, now let’s place our ActionVerbs Selector before one of these action methods, which one we want to call.
- I have placed the [HttpGet] before second action method and now because of this ActionVerbs Selector,URL Router will know which action method to call.
- Now when you open Get1 page, it will open fine.
- These ActionVerbs Selector are supported by ASP.NET MVC:
- Here’s the video demonstration of ActionVerbs Selector in detail: