w3resource

Action Selectors


In the previous tutorial, we learnt about the Action Method of the ASP.NET MVC. In this very tutorial we will be learning about action selectors, which are attributes that can be applied to the action methods.

Action selectors are more like helpers that helps the routing engine to select the right action method to handle a particular request. Action Selectors has the following attributes

  1. ActionName
  2. NonAction
  3. ActionVerbs

ActionName

The ActionName attribute is an attribute that allows you to give an ActionMethod a name different from the method name. This ActionName will now be used by the routing engine to complete user's request. The following example illustrates the use of ActionName.

public class ActivitiesController:Controller
{
    public ActivitiesController()
    {
    }  
    [ActionName("FetchActivities")]
    public ActionResult GetActivities(int id)
    {
        return View();
    }
}

In the above code snippet, we have an ActionName attribute "FetchActivities" attached to the "GetActivities" action method, hence literally the action name is now ?FetchActivities? instead of "GetActivites".

A request will now be made using the http://localhost/Activities/fetchActivities/1 instead of http://localhost/Activities/GetActivities/1

NonAction

One may ask, is there a way to make a function declared in the controller to be a non-action method? Yes, there is. Thanks to the NonAction selector attribute. This NonAction attribute indicates that a public method I a controller should not be treated as an action method. It helps us to writes normal methods for reusable operations in the controller.

The code snippet below illustrates the use of NonAction attribute in a controller function.

public class ActivitiesController : Controller
{
    public ActivitiesController()
    {
    }
    [NonAction]
    public Student GetActivities(int id)
    {
        return activityList.Where(s => s.ActivityId == id).FirstOrDefault();
    }
	
    public ActionResult ShowActivities(int id)
    {
        return View();
    }
}

In the above code snippet, GetActivities method and ShowActivites method will not be invoked the same way, as ShowActivities method is an action method while GetActivities method is not an action method.

Action Verbs

Action methods cannot be overloaded like other methods in C#. But with the help of action verbs, we can overload our action method. They help us select a different action method depending on the type of request.

There are a lot of action verbs in n ASP.NET MVC, but we will look at the e few action verbs that are extensively used and they are:

  • HttpGet
    It is used to handle GET Request.
  • HttpPost
    It is used to handle POST Request.

By default, every action method is treated as a Get Request, unless stated otherwise using an action verb.Let?s take an example.

Here, we?ve defined the two actions with different action verbs (GET, POST).

public class ActivitiesController : Controller  
{  
    public ActionResult Index()  
    {  
        return View();  
    }  
   
    [HttpPost]  
    public ActionResult Index(string name)  
    {  
        return View();  
    }  
} 

In the above code snippet, the index method uses an action verb to specify which method to be invoked whenever a particular request comes in.

There are some other types of Actions verbs you would like to know

  • HttpPut
    It is used for update requests
  • HttpDelete
    It is used for delete requests

Multiple Http requests can be applied to a method using to or more action verbs like shown in the following code snippet:

[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]  
public ActionResult Index(string name)  
{  
    return View();  
} 
 
public ActionResult Delete(int? id)  
{  
    return View();  
}  

[HttpPost, ActionName("Delete")]  
public ActionResult DeleteConfirmed(int id)  
{  
    return View();  
}  


Inviting useful, relevant, well-written and unique guest posts