Action And Action Selectors In ASP.NET MVC

Learn about Action and Action Selectors In ASP.NET MVC. Action methods are the methods which have one to one mapping with user requests. Action selectors are the attributes which can be applied to action methods.

Actions

Action methods are the methods which have one to one mapping with user requests. These methods execute requests and create aa response. Each Controller can have default action method, however that needs to be declared in RegisteRoutes() method of RouteConfig file.

Action Selectors

Action selectors are the attributes which can be applied to action methods. With the help of action selectors, routing engine selects the correct action method for handling specific request. Action selectors also decide the behaviour of method on the basis of name given in front of the action method.

There are three types of action selectors attributes,

  • ActionName
  • NonAction
  • ActionVerbs

ActionName

ActionName attribute allows you to use different name than the actual name of method.

Below is the sample for the same. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace FirstAspNetMVC.Controllers {  
  7.     public class EmplyeeController: Controller {  
  8.         // GET: Home  
  9.         public string Index() {  
  10.                 return "This is ASP.Net MVC ActionName";  
  11.             }  
  12.             [ActionName("GetDetail")]  
  13.         public string Detail() {  
  14.             return "This is detail at " + DateTime.Now.ToString("T");  
  15.         }  
  16.     }  
  17. }   

Now, in the above example, ActionName attribute is used as GetDetail; hence after running the application, you need to enter the following URL in the browser http://localhost:62833/Emplyee/ GetDetail

NonAction

NonAction is another built-in attribute which indicates that a method of a Controller is not an action method. It is used when you want a method not to be treated as an action method.

Below is the sample for the same.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. namespace FirstAspNetMVC.Controllers {  
  7.     public class EmplyeeController: Controller {  
  8.         // GET: Home  
  9.         public string Index() {  
  10.                 return "This is ASP.Net MVC ActionName";  
  11.             }  
  12.             [ActionName("GetDetail")]  
  13.         public string Detail() {  
  14.                 return "This is detail at " + GetTimeAsString;  
  15.             }  
  16.             [NonAction]  
  17.         public string GetTimeAsString() {  
  18.             return "This is detail at " + DateTime.Now.ToString("T");  
  19.         }  
  20.     }  
  21. }   

Now, in the above example, GetTimeAsstring() will be treated as non-action and it will not be available for accessing through URL.

ActionVerbs

You can restrict indication of a specific action to a specific HttpVerb. Now, with the help of action verbs, you can define two different methods of the same, however each one will have different action verb like one can have Http Get another can hove Http Post.

ASP.NET MVC framework supports the following ActionVerbs.

  • HttpGet - To fetch information from Server.
  • HttpPost- To post information on Server.
  • HttpPut - To updated existing information on Server.
  • HttpDelete- To delete existing information from Server.
  • HttpOptions- It represents a request for information about the communication options supported by web Server.
  • HttpPatch- For full or partial update of the resource.

Below is the sample for HttpGet and HttpPost.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using EmployeeMgrMvc.Models;  
  7. namespace EmployeeMgrMvc.Controllers {  
  8.         public class EmployeeController: Controller {  
  9.             public ActionResult Index() {  
  10.                 northwndEntities nw = new northwndEntities();  
  11.                 return View(nw.Employees);  
  12.             }  
  13.             public ActionResult Edit(int id) {  
  14.                     return View(GetEmployee(id));  
  15.                 }  
  16.                 [HttpPost]  
  17.             public ActionResult Edit(Employee emp) {  
  18.                 try {  
  19.                     northwndEntities nw = new northwndEntities();  
  20.                     Employee origEmp = GetEmployee(emp.EmployeeID);  
  21.                     nw.Employees.Attach(emp);  
  22.                     nw.ApplyOriginalValues("Employees", origEmp);  
  23.                     nw.SaveChanges();  
  24.                     return RedirectToAction("Index");  
  25.                 } catch {  
  26.                     return View();  
  27.                 }  
  28.             }  
  29.         }   

In the above example, we have created Edit action methods, one for HttpGet and another one for HttpPost.

Note

By default, ASP.NET MVC framework considers HttpGet on action methods.