Click here to Skip to main content
15,880,364 members
Articles / Web Development / ASP.NET

Custom Action Method Selector in MVC

Rate me:
Please Sign up or sign in to vote.
4.27/5 (7 votes)
28 Nov 2011CPOL2 min read 47.4K   488   20   7
Implementing a customized ActionMethodSelector attribute approach to make seemingly complex things trivial.

Introduction

The default routing algorithm for how the ASP.NET MVC framework invokes actions is like {controller}/ {action}/ {id} patterns.

But this becomes more complex when you have two methods with the same name, or when you invoke an action when form data is submitted, or execute a method only when an AJAX request is made.

Implementation

DisplayApplication.JPG

This can be done using ActionMethodSelector attributes. We generally use method selectors like:

  1. AcceptVerbs
  2. ActionName

AcceptVerbs

This attribute is used when we want to execute some action when a particular HTTP operation is performed like POST, GET, DELETE, etc. E.g.:

C#
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Employee employee)
{
      // To Do Code Here
}

The above method is executed when an HTTP POST operation is performed.

AcceptName

This attribute is used when you expose an action name with a different name than its method name, or you can use an action name attribute to expose two methods with the same name as the action with different names. E.g.:

C#
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit_GET(Employee  employee)
{
    // To Do Code Here
}
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Post)]
Public ActionResult Edit_POST(Employee employee)
{
    // To Do Code Here
}

Custom Action Method Selector

You can build your own ActionMethodSelector attributes by deriving from the abstract ActionMethodSelectorAttribute class.

This is an extremely simple class; you just need to override the method named IsValidForRequest (). If this returns false, this action method is not executed.

Now take a simple example of the Custom Action Method Selector. Develop an Ajaxmethod custom attribute useful for when you want to execute some action when an AJAX request is made.

The first step is to create an MVC application. Then add a class file named AjaxMethod.cs and inherit that class from ActionMethodSelectorAttribute. Then override a method named IsValidForRequest(). Your class will look like below:

C#
public class AjaxMethod:ActionMethodSelectorAttribute
{
   public override bool IsValidForRequest(ControllerContext controllerContext, 
          System.Reflection.MethodInfo methodInfo)
   {
       return controllerContext.HttpContext.Request.IsAjaxRequest();
   }
}

In the IsValidForReuest method, we check the Request object for the AJAX request.

Now we can use the Ajaxmethod attribute in our controller when you want to execute some action only when an AJAX request comes. For this, add a controller named NewsController into the controller folder. In this controller, we define two index actions: first index action only executes when a page is posted back and the second action is invoked when an AJAX request comes. Your controller will look like below:

C#
public class NewsController : Controller
{

    private readonly List<string> _news = new List<string>();
    private Random _rnd = new Random();

    public NewsController()
    {
        _news.Add("Moon Explodes");
        _news.Add("Stock Market up 200 percent");
        _news.Add("Talking Robort Created!");
    }
    //
    // GET: /News/

    public ActionResult Index()
    {
        var SelectedIndex = _rnd.Next(_news.Count);
        ViewData["News"] = _news[SelectedIndex];
        return View();
    }


    [AjaxMethod]
    [ActionName("Index")]
    public string Index_AJAX()
    {
        var SelectedIndex = _rnd.Next(_news.Count);
        return _news[SelectedIndex];
    }
}

Now add a View named index.aspx for displaying news and add the below markup into that:

HTML
<script type="text/javascript" src="../../Scripts/MicrosoftAjax.js"></script>
    <script type="text/javascript" src="../../Scripts/MicrosoftMvcAjax.js"></script>
      <%= Ajax.ActionLink("Get News", "Index", new AjaxOptions { UpdateTargetId = "news" })%>
      <span id="news"%><%:ViewData["News"] %></span>

Now run the application. You will find that when the page loads the first time the first index action executes and when you click on the GetNews link at that time, the second index action executes. The first time, it will go check the Ajaxmethod attribute but the IsValidForRequest method will return false and then it will execute the first index action, but when you click on the link at that time, IsValidForRequest returns true and executes the second action.

Conclusion

Some of the least customized but very useful extension points in ASP.NET MVC are action method selector attributes. But using them, we can make seemingly complex things trivial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I have been working as a Software Engineer on Microsoft .NET Technology.I have developed several web/desktop application build on .NET technology .My point of interest is Web Development,Desktop Development,Ajax,Json,Jquey,XML etc.I have completed Master of Computer Application in May-2011.I'm not happy unless I'm learning something new.

Comments and Discussions

 
BugActionName is just alias of method Pin
Afazal MD 310420930-Oct-13 23:09
professionalAfazal MD 310420930-Oct-13 23:09 
SuggestionTypo Error Pin
prawinkumarpauldavid30-Jan-13 16:16
prawinkumarpauldavid30-Jan-13 16:16 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey7-Jul-12 2:07
professionalManoj Kumar Choubey7-Jul-12 2:07 
QuestionVery well explained Pin
Dinesh Mani28-Nov-11 22:29
Dinesh Mani28-Nov-11 22:29 
AnswerRe: Very well explained Pin
Jigar Bagadai28-Nov-11 23:10
Jigar Bagadai28-Nov-11 23:10 
thanks
SuggestionA shorthand way to determine the accepted verb. Pin
stapia.gutierrez28-Nov-11 17:39
stapia.gutierrez28-Nov-11 17:39 
GeneralRe: A shorthand way to determine the accepted verb. Pin
Jigar Bagadai29-Nov-11 5:46
Jigar Bagadai29-Nov-11 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.