使用RouteValueDictionary重新路由到视图导致许多重定向... MVC3

时间:2012-05-31 13:38:11

标签: c# asp.net-mvc-3

会话过期后,我想将用户重定向回登录页面。所以我添加了一个类并继承自ActionFilterAttribute,这样我就可以随时检查一个动作即将被执行。作为测试,我把这段代码放在:

 public class SessionFilters : ActionFilterAttribute
 {
     public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
          var defaults = new RouteValueDictionary {{"Controller", "Home"}, {"Action", "About"}};
          filterContext.Result = new RedirectToRouteResult(defaults);
          base.OnActionExecuting(filterContext);
      }
  }

正如您所看到的,我正在强制重定向到“关于”视图,但我在浏览器中收到以下错误:

  The webpage at http://localhost:58494/Home/About has resulted in too many redirects.
  Clearing your cookies for this site or allowing third-party cookies may fix the 
  problem. If not, it is possibly a server configuration issue and 
  not a problem with your computer.

如何使用此方法正确地重定向到另一个视图?感谢

更新没关系。我有一个荷马辛普森时刻。我定义的是一个无限循环,所以浏览器就像“忘记这个”。代码正在运行..

1 个答案:

答案 0 :(得分:2)

确保没有使用[SessionFilters]属性装饰整个HomeController,而只修改需要重定向到的操作。如果你使用动作过滤器修饰整个控制器,那么显然这个过滤器适用于所有动作,包括关于因此无限重定向循环。