Custom ASP.NET MVC ActionFilterAttribute - hooks never get called
Asked Answered
A

7

16

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?

Aiguillette answered 28/6, 2011 at 17:5 Comment(3)
It seems all right. Which MVC version are you using?Chabot
This is MVC 2, I am beginning to think maybe something else is stopping filters from executions or something like that, can this be traceable like the entire execution flow somehow?Aiguillette
I had exactly the same issue. Even when I added the filter in the globalFilterCollection I received an error that I should implement the IActionFilter. After a while I found out that I was using the System.Web.**Http.Filters**.IActionFilter and System.Web.**Http.Filters**.ActionFilterAttribute instead of the correct System.Web.**Mvc**.IActionFilter and System.Web.**Mvc**.ActionFilterAttribute Mayby that it will help someone.Mildred
A
5

Finally figured out, it was in the end the fact that I have been putting the filter on a function that has in fact been an ActionResult function, but it was returned by another method that called it, so the filters are only being executed once on the entry point Action.

Aiguillette answered 29/6, 2011 at 19:42 Comment(0)
G
29

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());
  }
}
Genarogendarme answered 29/6, 2011 at 9:22 Comment(0)
U
20

My error was that I referenced System.Web.Http.Filters, not System.Web.Mvc

Unbowed answered 7/4, 2013 at 15:4 Comment(3)
Thank you very much Yara. I also committed the same mistake and I learned from your comment. Hence one up vote to you. :-)Bosh
Seems to be the first reference resharper goes forDuumvir
same problem here! thanks for the tip. Was a mistake by ResharperSororicide
A
5

Finally figured out, it was in the end the fact that I have been putting the filter on a function that has in fact been an ActionResult function, but it was returned by another method that called it, so the filters are only being executed once on the entry point Action.

Aiguillette answered 29/6, 2011 at 19:42 Comment(0)
R
1

Have you tried overriding the OnActionExecuting like:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
     base.OnActionExecuting(filterContext);
}

This is the way I write action filters and haven't had a problem with them being called.

Rollicking answered 28/6, 2011 at 17:22 Comment(2)
Have tried overriding the action executing instead did not seem to make any difference... it was still not getting called Could there be any further configuration required like in a web.config or anything like that?Aiguillette
@Aiguillette - There is no web.config entry required. Are you sure your action is being called? Have you set a breakpoint in your filter to see if that gets hit?Rollicking
M
1

Your code generally looks good to me. It could be related to what you are doing (or not doing) in your Action method. If you aren't returning a view, etc., it's possible your "ResultExecuting" event handler isn't being called. I would grab the sample here and see what gets logged for your action.

Masturbate answered 28/6, 2011 at 17:23 Comment(0)
I
0

Ideas:

Are you positive your filter isn't running? Have you put a break point in it? Are you sure it's not throwing and exception? Are you sure the action you decorated is actually gettting called?

Different Implementation:

Override the OnResultExecuting method of your controller.

Individuality answered 28/6, 2011 at 17:19 Comment(1)
Yes what i actually did is have a breakpoint on both my ActionResult function and on my custom filter, the debugger only would reach the breakpoint on the ActionResult function howeverAiguillette
S
0

If you see the inheritance of class 'ActionFilterAttribute' that inherits "FilterAttribute, IActionFilter, IResultFilter" classes. The method you are looking is available in IResultFilter interface.

Therefore it never executes a method when we use custom class as an attribute. Always we need to override all IActionFilter and IResultFilter methods in-terms to custom implementation.

Stinkstone answered 28/4, 2015 at 9:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.