Sunday, March 20, 2011

Can I skip an ActionFIlter in ASP.NET MVC?

I have an action filter attribute on a base class all my controllers inherit from. I want it (the filter) to work on all methods EXCEPT one. Can it be done? How?

From stackoverflow
  • Not sure around the details, but could you set/override a member variable in your controller to indicate this? Then have your filter (or base controller) simply tell the filter not to run if that variable is present.

  • This is a little hackish, but you could test for the action in the filter's OnActionExecuting method, like so:

    var controllerName = filterContext.RouteData.Values["controller"].ToString();
    var actionName = filterContext.RouteData.Values["action"].ToString();
    if (controllerName == "Foo" && actionName == "Bar")
    {
        return;
    }
    //do normal stuff
    
    Todd Smith : The other option is to create a custom Action Attribute which encapsulates this functionality. I did that with a custom Authorize Attribute which skips the Login method on my account controller.

0 comments:

Post a Comment