Custom ASP.NET MVC ActionFilterAttribute - hooks never get called

前端 未结 7 521
故里飘歌
故里飘歌 2021-01-01 18:50

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:

相关标签:
7条回答
  • 2021-01-01 19:23

    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.

    0 讨论(0)
  • 2021-01-01 19:28

    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.

    0 讨论(0)
  • 2021-01-01 19:41

    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.

    0 讨论(0)
  • 2021-01-01 19:41

    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.

    0 讨论(0)
  • 2021-01-01 19:42

    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());
      }
    }
    
    0 讨论(0)
  • 2021-01-01 19:44

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

    0 讨论(0)
提交回复
热议问题