Action Verbs And Action Selectors In ASP.NET

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.

  1. public class StudentsController : Controller  
  2. {  
  3.     // GET: Students  
  4.     public ActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8.    
  9.     [NonAction]  
  10.     public Student GetStudent()  
  11.     {  
  12.         return new Student();  
  13.     }  
  14. }  

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.

  1. public class StudentsController : Controller  
  2. {  
  3.     // GET: Students  
  4.     public ActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8.    
  9.     [ActionName("details")]  
  10.     public ActionResult Description()  
  11.     {  
  12.         return View();  
  13.     }  
  14. }  

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.

ASP.NET

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).

  1. public class StudentsController : Controller  
  2. {  
  3.     // GET: Students  
  4.     public ActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8.    
  9.     [HttpPost]  
  10.     public ActionResult Index(string name)  
  11.     {  
  12.         return View();  
  13.     }  
  14. }  

And, we’ve add the ‘Index’ view as -

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.    
  5. <form action="" method="post">  
  6.     <div class="form-group">  
  7.         <label for="name">Name</label>  
  8.         <input type="text" id="name" class="form-control" name="name" />  
  9.     </div>  
  10.     <button type="submit" class="btn btn-primary">Submit</button>  
  11. </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.

ASP.NET

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. 

  1. [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]  
  2. public ActionResult Index(string name)  
  3. {  
  4.     return View();  
  5. }  

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. 

  1. // GET: Admin/Products/Delete/5  
  2. public ActionResult Delete(int? id)  
  3. {  
  4.     return View();  
  5. }  
  6.   
  7. // POST: Admin/Products/Delete/5  
  8. [HttpPost, ActionName("Delete")]  
  9. public ActionResult DeleteConfirmed(int id)  
  10. {  
  11.     return View();  
  12. }  


Similar Articles