【问题标题】:your application has redirected loops in MVC您的应用程序在 MVC 中重定向了循环
【发布时间】:2014-11-19 20:41:54
【问题描述】:

在 global.asax 中编写代码后出现此错误。如果我保留一个断点来检查它是否正在触发和旋转,最终浏览器结果是否具有上述响应 [“您的应用程序已重定向循环”]。 `

 public class SessionExpireAttribute : ActionFilterAttribute
       {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        // check  sessions here
        if (HttpContext.Current.Session["username"] == null)
        {
            filterContext.Result = new RedirectResult("~/Account/Login");
            return;
        }

        base.OnActionExecuting(filterContext);
    }
}`

真的很有趣,但是为什么这个愚蠢的错误会一次又一次地发生。有什么想法吗?

【问题讨论】:

  • 当您的应用程序启动时,Session["username"] 为空,因此它会执行重定向,从而触发 ActionFilter。该值仍然为空,因此它会执行重定向,从而触发 ActionFilter。该值仍然为空,所以它....

标签: asp.net-mvc asp.net-mvc-4 session


【解决方案1】:

AccountController 中删除此ActionFilter

【讨论】:

    【解决方案2】:

    当您重定向到“~/Account/Login”时,将再次调用 OnActionExecuting 的代码,因此 HttpContext.Current.Session["username"] == null 为真,您就有了重定向循环。

    所以,你需要其他条件来检查这个

    【讨论】:

      【解决方案3】:

      对不起,我没有正确阅读您的问题

      public class SessionExpireAttribute : ActionFilterAttribute
             {
            public override void OnActionExecuting(ActionExecutingContext filterContext)
          {
              HttpContext ctx = HttpContext.Current;
      
              // check  sessions here
              if (HttpContext.Current.Session["username"] == null)
              {
                  filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                          { "Controller", "Accounts" },
                          { "Action", "Login" }
                          });
      
              }
      
              base.OnActionExecuting(filterContext);
          }
      }
      

      希望这行得通。

      【讨论】:

      • 帐户控制器中的 Login ActionResult 已保留 [AllowAnonymous] 属性
      • 你能告诉我所有的属性都应用于登录页面吗?以什么顺序?
      • 控制器级别的[会话过期]。
      • 是的,它可以正常工作,但我将会话超时保持在 1 分钟,以前当用户在 1 分钟后单击时,它曾经因为会话空而显示错误,但现在没有错误,但还有一个问题是页面没有重定向到登录。它在同一页面中
      • [Authorize] [InitializeSimpleMembership] 公共类 AccountController : BaseController { Repository _rep = new Repository(); [AllowAnonymous] public ActionResult Login(string returnUrl)
      猜你喜欢
      • 2020-11-14
      • 2014-01-11
      • 1970-01-01
      • 2012-06-07
      • 2019-03-20
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      相关资源
      最近更新 更多