Custom ASP.NET MVC ActionFilterAttribute - hooks never get called

aromasca picture aromasca · Jun 28, 2011 · Viewed 14.5k times · Source

Hi I`m trying to do something that seems kinda easy, and is documented that way but for some reason its not going that easy.

Basiclly I wrote something like this:

public class CacheControllAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    { 
        //do something
        base.OnResultExecuting(filterContext);
    }
}

However when i try and use this on an action result like this:

[CacheControllAttribute]
public ActionResult SomeAction()
{
    //whatever
}

My custom overriden function never gets called...

any ideas on this? or how to implement this differently?

Answer

David McLean picture David McLean · Jun 29, 2011

A probably silly suggestion but did you add it to your global.asax?
This is an example from one of my apps:

public class MvcApplication : System.Web.HttpApplication     
{
  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
  {
    filters.Add(new LogonAuthorize());
    filters.Add(new HandleErrorAttribute());
  }
}