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:
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?