3

如何正确应用全局动作过滤器,使其触发所有动作,然后返回触发过滤器的自定义动作结果?我将提供到目前为止我所做的事情的示例,但无法触发filterContext.Result = ...

全球.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);
     }
}

如何从这个上下文中正确重定向到 LogOff() 方法,使用 Result 或其他方式?谢谢!

4

1 回答 1

1

根据this answer on a similar question,您需要将Area参数分配给空字符串,例如

var values = new RouteValueDictionary(new
{
    controller = "Account",
    action = "LogOff",
    area = ""
});
于 2012-07-30T13:05:53.277 回答