The MVC platform: interacting with the ActionMethodSelectorAttributes

In the one of the latest”s posts, we’ve seen how we can use the AcceptVerbsAttribute to limit the valid HTTP methods that can be used for requesting the execution of an action method. Today we’ll take a closer look at how these  items are used at runtime (ie, we’ll see how they are used internally by the platform). Lets start by looking at the ActionMethodSelectorAttribute class. In the  RC version, it’s really a simple abstract class with only one method:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ActionMethodSelectorAttribute : Attribute {
   public abstract bool IsValidForRequest(
                                 ControllerContext controllerContext, MethodInfo methodInfo);

The idea is to insert a base class that you can extend so that you can interact with the runtime and check if some precondition is valid before executing an action method. Now, the real question is when will these classes be called and who is responsible for finding them and using them at runtime.

If you’ve been reading this series, you already know that I’ve written about the IActionInvoker and its default ControllerActionInvoker implementation. In the RC release, there have been some changes (don’t forget that I’ve written that post a long time ago). Internally, ControllerActionInvoker,InvokeAction will defer most of the work into two classes: ControllerDescriptor and ActionDescriptor (which is obtained from the ControllerDescriptor).

In fact,the main responsability of the ControllerDescriptor (by default,you’ll be using the type ReflectedControllerDescriptor) is getting an ActionDescriptor that “abstracts” the action that should be executed for the current request.

Internally, when the ReflectedControllerDescriptor tries to get an ActionDescriptor (btw, you’ll get a ReflectedActionDescriptor),  it ends up running the selection filters that have been applied to the current action method (if any, that is). And that’s how your custom ActionMethodSelectorAttributes end up running. There’s one interesting thing regarding the internal implementation of this feature: if any of the applied ActionMethodSelectorAttributes don’t return true when the IsValidForRequest method is executed, then the FindAction method of the ReflectedControllerDescriptor returns null as if the action you requested didn’t exist. This ends up the current request processing and returns 404 to the client.

The important thing to keep in mind is that if you apply a ActionMethodSelectorAttribute to a method, it will run before all the existing filters and if its IsValidForRequest method returns false, it will end up the processing of the request and none of your filters or action code will ever execute. Keep this in mind when you create your own ActionMethodSelectorAttribute derived classes.

And that’s it for today. Keep tuned for more on the MVC platform.


~ by Luis Abreu on February 4, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: