In this tutorial, we will learn about ASP.NET MVC Selectors. ASP.NET MVC selectors are the attributes that can be applied to the action methods. They help routing engine to select correct action method to handle a request URL. MVC 5 includes three following action selectors attribute.
ASP.NET MVC selectors attribute.
Let’s explain all the three types of MVC Selector one by one by creating new project.
Step#1: Open Visual Studio, Click on file, New and then Project (File => New => Project).
Step#2: After showing new project dialog, select templates from the left pane, choose the web under Visual C# (Templates => Visual C# => Web). Then choose “ASP.NET Web Application” from the middle pane. Then enter the project name (e.g “SelectorsDemo”) in the name field, and then set the location of your project in the location Field, and then finally click “OK” button. It will show a new dialog.
Step#3: Choose the empty project, and then check the MVC check box, and then finally click “OK” button. It will create simple ASP.NET MVC application.
Step#4: To Add a controller, right-click on the controller folder in the solution explorer and then choose Add, and then Controller (Add => Controller). It will show the Add scaffold dialog.
Step#5: Now select the ‘MVC 5 Controller – Empty’ option. And then click “Add” button. Then Add Controller dialog will appear.
Step#6: Enter the name (E.g “Home.cs”) of controller in the Controller Name field and then click “Add” button.
Finally, you will see a new “Home.cs” file in the controller folder.
How to Implement ASP.NET MVC Selectors attribute?
ActionName attribute is used for the name of an action. It also allows us to specify a different action name than the actual method name. Let’s explain with example.
Let’s modify Index method of HomeController.cs class.
So, in the above example we have applied ActionName(“Name”) attribute to the StudentName action method. Now run your application with this URL (http://localhost:54860/Home/Name) Instead of this http://localhost:54860/Home/StudentName, Otherwise you will get an error.
NonAction attribute is indicates that a method of a controller is not an action method. It is used when you want to public method a controller but do not want to treat it as an action method.
Let’s take an example
Add an action method (E.g RollNumber) in the HomeController.cs class
Now run your application with this URL http://localhost:54860/Home/RollNumber , But you will get an error. It is because you can’t use RollNumber method as an action in URL.
Note:- If you want to access RollNumber method, then you must call it within another action method. Let’s have a look at simple example.
Let’s run your application with this URL http://localhost:54860/Home/Name, you will get the following output.
The ActionVerbs selector is used when you want to control the selection of action method based on an Http request method. E.g. you can define two different action methods with the same name but one action method responds to an HTTP Get Request and the other one responds to an HTTP Post Request.
MVC Framework supports different kind of ActionVerbs, such as HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions, and HttpPath. You can apply these attribute to action method to indicate the kind of Http request the action method supports.
Note:- If you don’t apply any of these attribute then it considers action method as a HttpGet request.
Let’s take a look at simple example.
Add the following two action methods with the same name in the HomeController.cs Class.
Note:- Now run your application with this URL http://localhost:54860/Home/Student, but you will get an error just because you have two action method with the same name and the MVC Framework is unable to figure out which action method should be picked up for the request.
Now, let’s modify these action methods with the following code.
Now run your application with this URL http://localhost:54860/Home/Student, you will get the Following output.
Let’s take a look at how to implement all other ActionVerbs.
Note:- You can also apply multiple Http Verbs using AcceptVerbs Attribute. Let’s take a simple example
GetAndPostStudentAction method supports both Get and Post ActionVerbs.
Please Like & Share this article!