Action Selectors

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();
    }
}

ActionVerbs: HttpGet, HttpPost, HttpPut

The ActionVerbs selector is to handle different type of Http requests. The MVC framework includes HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions, and HttpPatch action verbs. You can apply one or more action verbs to an action method to handle different HTTP requests. If you don't apply any action verbs to an action method, then it will handle HttpGet request by default.

The following table lists the usage of HTTP methods:

Http method Usage
GET To retrieve the information from the server. Parameters will be appended in the query string.
POST To create a new resource.
PUT To update an existing resource.
HEAD Identical to GET except that server do not return the message body.
OPTIONS It represents a request for information about the communication options supported by the web server.
DELETE To delete an existing resource.
PATCH To full or partial update the resource.

Visit W3.org for more information on Http Methods.

The following example shows how to handle different types of HTTP requests in the Controller using ActionVerbs:

Example: Handle HTTP Requests in the Controller
public class StudentController : Controller
{
    public ActionResult Index() // handles GET requests by default
    {
        return View();
    }

    [HttpPost]
    public ActionResult PostAction() // handles POST requests by default
    {
        return View("Index");
    }


    [HttpPut]
    public ActionResult PutAction() // handles PUT requests by default
    {
        return View("Index");
    }

    [HttpDelete]
    public ActionResult DeleteAction() // handles DELETE requests by default
    {
        return View("Index");
    }

    [HttpHead]
    public ActionResult HeadAction() // handles HEAD requests by default
    {
        return View("Index");
    }
       
    [HttpOptions]
    public ActionResult OptionsAction() // handles OPTION requests by default
    {
        return View("Index");
    }
       
    [HttpPatch]
    public ActionResult PatchAction() // handles PATCH requests by default
    {
        return View("Index");
    }
}

You can also apply multiple action verbs using the AcceptVerbs attribute, as shown below.

Example: AcceptVerbs
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult GetAndPostAction()
{
    return RedirectToAction("Index");
}

Sandip Mhaske

I’m a software developer exploring the depths of .NET, AWS, Angular, React, and digital entrepreneurship. Here, I decode complex problems, share insightful solutions, and navigate the evolving landscape of tech and finance.

Post a Comment

Previous Post Next Post