How to apply Global ActionFilter to all controllers and return Result?

4.2k Views Asked by At

How to properly apply global action filter so it triggers on all actions, and then returns Custom result of action that triggered the filter? I will provid example of what i have done so far, but been unable to trigger the filterContext.Result = ...

Global.asax

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new RequestCounter());
}

public class RequestCounter : ActionFilterAttribute
{

    public override void OnResultExecuted(ActionExecutingContext filterContext)
    {  
          if (!LogCounter())
          {
               if (!filterContext.IsChildAction)
               {
                   var values = new RouteValueDictionary(new
                   {
                       controller = "Account",
                       action = "LogOff"
                   });

                   filterContext.Result = new RedirectToRouteResult(values);

                   //--> Here, the action is not redirected to LogOff method,
                   //    the goal is to logoff user, program just continues???
               }
          }

          base.OnResultExecuted(filterContext);
     }
}

How to properly redirect to LogOff() method, from this context, using Result or some other way as well?? thanks!

1

There are 1 best solutions below

0
On

According to this answer on a similar question, you need to assign the Area parameter to an empty string e.g.

var values = new RouteValueDictionary(new
{
    controller = "Account",
    action = "LogOff",
    area = ""
});