ASP.NET MVC 3: Global action filters

ASP.NET MVC 3 supports global action filters. Global action filters are applied to all actions in web application. By example, you can use global action filters for common security checks. In this posting I will show you how to write dummy action filter, register it as global and test it.

NB! Sample code for this posting is available at my examples GitHub repository as Visual Studio solution.

Creating action filter

Let’s start with our primitive action filter that is able in some cases ruin page layout for IE. Am I evil? Yes, I am! But I want this example to work on IIS and Cassini both so let’s make something trivial.

public class MyActionFilterAttribute : ActionFilterAttribute
{
   
public override void OnResultExecuting(ResultExecutingContext
context)
    {
       
base
.OnResultExecuting(filterContext);

        context.RequestContext.HttpContext.Response.Write(
"<!-- Buuu! -->");
    }
}

All this evil filter does is it writes string <!—Buuu! –> to response stream, so it is the first line of output.

No action filter outputNow open one of your ASP.NET MVC projects and run it. It does not matter if you run it on IIS or Cassini (ASP.NET development web server). If you look at page source you should see usual HTML there. Something like you see on image on right

Registering global action filter

Now, without touching any controller let’s put our evil Buuu! in place. Open global. asax and modify Application_Start event so it looks like follows.

protected void Application_Start()
{
   
AreaRegistration
.RegisterAllAreas();

   
// Register global filter
    GlobalFilters.Filters.Add(new MyActionFilterAttribute
());

    RegisterGlobalFilters(
GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

Compile your project and run application again. Now you should see output like this.

Global action filter output is here

Without any additional modification to controllers and their methods we got our action filter work for all controllers. You can surf around your site and for every request you should see now my evil message.

Conclusion

Global application filters are powerful and easy to use features. You can use global filters for different purposes like establishing global security policies and controlling output. Of course, you may find many other uses for global action filters. ASP.NET MVC makes is very easy to register filters at global level and I think it is another great addition to MVC framework.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    4 thoughts on “ASP.NET MVC 3: Global action filters

    • May 14, 2011 at 4:20 am
      Permalink

      Thanks for an idea. I have an application that needs to be https always and this is a great starting point for me.

    • November 16, 2011 at 8:12 am
      Permalink

      Very simple, yet powerful explanation of the Global Action Filter concept. Thanks. I wish you had kind of Google+ on your page, so that we could easily share your articles.

    • April 26, 2012 at 2:57 pm
      Permalink

      ASP.NET MVC Filters are one of the greatest features of ASP.NET MVc, and I think the reason is that, you can get a strategic point in response, where everything is in your control (a centralized place) to do whatever you like.

      However, getting the result HTML (what that is being sent back to the user) is not a neat, clean, straightforward matter. I think explaining a little about that would be a good idea in general.

      Thanks.

    • Pingback:ASP.NET MVC 3: Using global action filters to find out running time of controller actions | Gunnar Peipman - Programming Blog

    Leave a Reply

    Your email address will not be published. Required fields are marked *