Action Selectors
Action Selector is an attribute that applies to action methods. It helps the routes to identify the action based on the request. Here, in an ASP.NET environment, we have a few Action Selectors.
- NonAction
- ActionName
- ActionVerbs
NonAction
Let’s suppose, we want to limit an action to not be treated as an action. We’ve just made it for the purpose of code reusability that’s why we declare this action as NonAction.
- public class StudentsController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
-
- [NonAction]
- public Student GetStudent()
- {
- return new Student();
- }
- }
Now, we can’t request GetStudent() into our browser.
ActionName
ActionName attribute is used to change the name of the action at runtime. Let’s see an example to understand.
- public class StudentsController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
-
- [ActionName("details")]
- public ActionResult Description()
- {
- return View();
- }
- }
We have different action names in the Controller but we have changed the name of the action and now when we’ll request this action then we’ll use ActionName attribute value.
Action Verbs
We can’t overload our action methods like we do in C#. But with the help of action verbs, we can overload our action method. We select a different action method depending upon the request with the help of action verbs.
Here, in ASP.NET MVC, we have few action verbs but they are extensively used in Web APIs. But here, in MVC, we normally use two action verbs.
- HttpGet
It is used to handle GET Request.
- HttpPost
It is used to handle POST Request.
But if you don’t apply any action verb, then this action will be treated as GET request action.
Let’s take an example.
Here, we’ve defined the 2 actions with different action verbs (GET, POST).
- public class StudentsController : Controller
- {
-
- public ActionResult Index()
- {
- return View();
- }
-
- [HttpPost]
- public ActionResult Index(string name)
- {
- return View();
- }
- }
And, we’ve add the ‘Index’ view as -
- @{
- ViewBag.Title = "Index";
- }
-
- <form action="" method="post">
- <div class="form-group">
- <label for="name">Name</label>
- <input type="text" id="name" class="form-control" name="name" />
- </div>
- <button type="submit" class="btn btn-primary">Submit</button>
- </form>
Look, here, in this code, when we request the index action, the GET request comes in and triggers the GET Index action but when the form posts back then the request is actually posted so it comes to the POST action of Index action. This is how the request works and triggers the action.
This is how action method works with HTTP GET and POST request.
There are some other types of Actions verbs which also exist like
HttpPut for updation
HttpDelete for deletion
But we commonly use get and post a request in our Controller Actions.
We can also apply multiple HTTP requests on a single action.
- [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
- public ActionResult Index(string name)
- {
- return View();
- }
Now, it will handle both the get and post request.
There are some key points to keep in mind.
- Action methods can’t be private or protected they must be public.
- Action methods can’t be static.
We can’t override the Action Methods like we do in C#. To override the Action Methods, we need to use [HttpGet, HttpPost] Action Verbs or use the attribute of [ActionName(“”)]
As we do in Delete Scenario when making the CRUD because both Delete Methods have the same signature, so also we change the name of the second Delete method as DeleteConfirmed and apply the ActionName attribute upon it.
-
- public ActionResult Delete(int? id)
- {
- return View();
- }
-
-
- [HttpPost, ActionName("Delete")]
- public ActionResult DeleteConfirmed(int id)
- {
- return View();
- }