ActionFilter Overkill

We've seen not one, but two posts this week where we used an ActionFilter as the solution to a problem. ActionFilters are powerful. ActionFilters can inspect and modify action parameters and action results. They can cover almost any cross-cutting concern in ASP.NET MVC: caching, security, auditing, etc.

But, should you use them everywhere?

[Log, UnitOfWork, PreParseParams]
[CommonData, VerifyRedirects, HandleError]  
public class BusyController : Controller
{
    
}

Think about the other options:

HttpModules see the big picture. They can hook into the beginning of a request before a request even knows it's destined for a controller. Modules are configurable at runtime and can drop into any web site as a completely decoupled component (example: ELMAH).

Custom ModelBinders can build and tweak models, build action parameters, and provide some validation support. I'd use a model binder instead of an action filter if I wanted to change or inspect model or parameter values (see: tips for model binding).

Custom ActionResults can encapsulate what happens after an action finishes executing. Modify HTTP headers, set response properties, and more (example: custom ImageResult).

Don't default to using an ActionFilter if another solution is a better fit.

Print | posted @ Thursday, July 01, 2010 9:12 PM

Comments on this entry:

Gravatar # re: ActionFilter Overkill
by Jake Scott at 7/2/2010 4:55 AM

The MvcExtensions project on codeplex has a fluent api for registering action filters http://mvcextensions.codeplex.com/

weblogs.asp.net/...
  
Gravatar # re: ActionFilter Overkill
by Scott at 7/2/2010 10:15 AM

@Jake - it's not as much as issue of how to register the action filters - it's more about using action filters in places where a model binder or custom action result might be appropriate.
  
Gravatar # re: ActionFilter Overkill
by Surge at 7/3/2010 1:00 PM

good info thanks
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 5 and 2 and type the answer here: