SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (5)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box

A Change to the MVC "ActionSelectionAttribute"?

(Sep 04 2008 - 07:47:34 AM by Timothy Khouri) - [print blog post]

ASP.NET MVC currently has the notion of an ActionSelectionAttribute that allows you to aid the MVC framework in choosing which method should handle the Action that the user is choosing. Meaning, if you wanted the user to see the action "/Members/Signup" for both displaying the form, and also handling the posting of the form... but you wanted that to be handled by two different methods in your controller, you would do the following:

[AcceptVerbs("GET")]
public ActionResult Signup()
{
   // ...

}

[AcceptVerbs("POST")]
[ActionName("Signup")]
public ActionResult Signup_Submitted()
{
   // ...

}

This is great, but there is a slight problem (as I see it) with the current implementation of how the default controller action invoker uses the ActionSelectionAttribute.

Basically, these attributes have to filter down to only *one* possible result. Meaning, you can't have two methods that could potentially handle the same request. So, this would cause an error:

// This will error, because both this method, and the one
// below can handle a "POST".

public ActionResult Signup()
{
   // ...

}

[AcceptVerbs("POST")]
[ActionName("Signup")]
public ActionResult Signup_Submitted()
{
   // ...

}

I would personally like to see this attribute modified slightly to allow a "precedence" value. This way, if multiple methods can be selected, MVC can figure out which *one* should be used instead of throwing an error.

This may not seem like a big deal with the scenario above, but imagine a more complicated one:

[ActionName("DisplayLetter")]
[AcceptHeaders("application/ms-word", Precedence=1)]
public ActionResult DisplayLetter_AsWord(int letterID)
{
   // ...

}

[ActionName("DisplayLetter")]
[AcceptHeaders("application/pdf", Precedence=2)]
public ActionResult DisplayLetter_AsPdf(int let)
{
   // ...

}

public ActionResult DisplayLetter(int let)
{
   // ...

}

Now it all starts to make a little more sense... well, to me at least. A request could come in from a user who accepts both ms-word and pdf's!!! So, which method gets called? Ahh, the precedence will determine that!

Any thoughts?

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

Blog Archives

  • June 2009 - (1)
  • January 2009 - (1)
  • November 2008 - (1)
  • October 2008 - (2)
  • September 2008 - (2)
  • August 2008 - (3)
  • July 2008 - (1)
  • June 2008 - (3)
  • May 2008 - (2)
  • April 2008 - (2)
  • March 2008 - (4)
  • February 2008 - (2)
  • December 2007 - (2)
  • November 2007 - (1)
  • October 2007 - (4)
  • September 2007 - (9)
  • August 2007 - (7)

Related Ads

SingingEels.com as of Mar 15 2010 - 03:02:40 PM - (0.0781295)