本文介绍了错误:"此网页有重定向循环"在asp.net的MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC中,我创建自定义属性为会话超时,如:

In MVC, I have created custom attribute for Session Timeout, like:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        // If the browser session or authentication session has expired...
        if (ctx.Session["USER_ID"] == null )
        {
            filterContext.Result = new RedirectToRouteResult(
                   new RouteValueDictionary {
                    { "Controller", "Employer" },
                    { "Action", "SessionTimeout" }
            });

        }

        base.OnActionExecuting(filterContext);
    }
}

我的意思是,当会话超时,它应该重定向到SessionTimeout行动的方法,我已经放置控制器的方法,比如,

I mean is when session is time out, it should to redirect to "SessionTimeout" Action method, and I have placed for Controller methods like,

 [SessionExpireFilter]
  public class EmployerController : Controller
  {
   //Action Methods   
  }

但在这里,当会话超时是,它是将操作方法​​在浏览器的地址,但它显示如下:

But here, when Session is Timeout, it is going to action method on browser address, but it is displaying like:

 This webpage has a redirect loop

我不知道我要去的地方错了?

I don't know where I am going wrong?

推荐答案

SessionTimeout 操作导致 OnActionExecuting SessionExpireFilter 再次运行,这将成为无限循环。您可以:

Your SessionTimeout action causes OnActionExecuting in SessionExpireFilter to run again, which becomes an infinite loop. You can either:


  • 在它自己的控制器将 SessionTimeout 动作,而在 SessionExpireFilter 属性

  • SessionExpireFilter 属性除了个人行为,而不是控制器 SessionTimeout

  • 检查,如果当前操作是 SessionTimeout OnActionExecuting code和不执行重定向(不是最好的主意,因为它的名字可以在将来改变,并再次造成麻烦)。

  • Put SessionTimeout action in its own controller without the SessionExpireFilter attribute
  • Put SessionExpireFilter attribute to the individual actions except SessionTimeout, instead of the controller
  • Check if the current action is SessionTimeout in your OnActionExecuting code and don't perform the redirect (not the best idea because its name can change in the future and cause trouble again).

这篇关于错误:"此网页有重定向循环"在asp.net的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 17:20