Action Filters – ASP.NET MVC Framework

27 Mar
2011

Before I write some code and sample oriented articles about ASP.NET MVC Framework Action Filters. I want to make a quick introduction and talk about filters. You may need to do something before or after your action methods get executed. ASP.NET MVC Framework provides some level of filters to do that kind of things. Filters are not different then normal classes in .NET, and you can use declarative programming features to active this special classes, filters.

Types of ASP.NET Filters

Authorization Filters : These are for performing authorization logics on action methods. Implements IAuthorizationFilter interface. Built-in samples : AuthorizeAttribute and RequireHttpsAttribute.

Action Filters : Implements IActionFilter interface and provides OnActionExecuting and OnActionExecuted methods to perform things before and after of action methods get executed.

Result Filters : Implements IResultFilter and provides OnResultExecuting and OnResultExecuted methods to perform additional logics before and after ActionResult objects get executed. Builtin sample : OutputCacheAttribute, this attribute can returns cached result before action method get executed.

Exception Filters : Implements IExcetionFilter and gets executed if an unhandled exception occurs on action method. You can log and create custom result if unhandled exception occurs.

Filter objects don’t keep state, they are transient objects and for each request they get initialized and disposed when request completes.

If you set some value to the ActionExecutingContext.Result property in OnActionExecuting and OnResultExecuting methods pending filters will not be executed.

You can use existing xxxAttribute classes which implement required interfaces and inherits from Attribute. FilterAttribute (base of all), ActionFilterAttribute, AuthorizeAttribute, HandleErrorAttribute.

Controller base class implements all this interfaces by default so it has all these 6 method implementations and it does nothing by default but if you want to override one of them you can create your own base class and override it. That means instead of creating your custom filter with required  implementation and decorating your controllers with that attribute you can create a base controller and override methods.

But we should keep in my mind overridden methods get executed before all custom created filter attributes regardless of Order value in attributes.

Now you have this question : When we should create and use filter attributes or when should we override methods from controller class?

If you don’t have a custom base controller class for each controller and if you want to reuse filter attributes in all controllers use filter attributes. If someone asks my opinion, I prefer to create different attributes to have a better design.  And this doesn’t mean that you need to apply your filters to all your controllers separately. I would still prefer to have a custom base controller class and decorate my custom filter attributes on the base class level if I want to apply an attribute to all controllers. Check Layer Super Type design pattern from Martin Fowler’s Patterns of Enterprise Application Architecture book. (http://www.martinfowler.com/eaaCatalog/layerSupertype.html)

Before finishing this article I want to mention about Order’s of attributes. Normally your filter attributes should not be depended on each others execution but it’s better to know when these are get executed.

Executin order of filters :

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

If you have more filters applied for a filter type, Order property defines execution order of filters.

I am planning to write some samples for filters and I hope this quick introduction can help some people to understand the concept.



2 Responses to Action Filters – ASP.NET MVC Framework

Avatar

» ASP.NET MVC 3–Global Action Filters develoq.net

July 23rd, 2011 at 4:36 am

[...] one of my previous blog post I mentioned about ASP.NET MVC action [...]

Avatar

Ingol

July 30th, 2012 at 4:59 pm

Why is that ActionFilterAttribute’s prop Result in OnResultExecuted or any other event not doing as the same as it is on AuthorizeAttribute. eg. not redirecting to specific action if RedirectToRouteResult is provided?

Comment Form

top