Action Selectors

Action selector is the attribute that can be applied to the action methods. It helps the routing engine to select the correct action method to handle a particular request. MVC 5 includes the following action selector attributes:

  1. ActionName
  2. NonAction
  3. ActionVerbs

ActionName

The ActionName attribute allows us to specify a different action name than the method name, as shown below.

Example: Specify a different action name
public class StudentController : Controller
{
    public StudentController()
    {
    }
       
    [ActionName("Find")]
    public ActionResult GetById(int id)
    {
        // get student from the database 
        return View();
    }
}

In the above example, we have applied ActioName("find") attribute to the GetById() action method. So now, the action method name is Find instead of the GetById. So now, it will be invoked on http://localhost/student/find/1 request instead of http://localhost/student/getbyid/1 request.

NonAction

Use the NonAction attribute when you want public method in a controller but do not want to treat it as an action method.

In the following example, the Index() method is an action method, but the GetStudent() is not an action method.

Example: NonAction
public class StudentController : Controller
{
    public string Index()
    {
            return "This is Index action method of StudentController";
    }
   
    [NonAction]
    public Student GetStudent(int id)
    {
        return studentList.Where(s => s.StudentId == id).FirstOrDefault();
    }
}