从Application_Error重定向页面时出错

时间:2015-04-16 12:25:31

标签: c# redirect model-view-controller session-per-request

我试图进行重定向,我有一个单例类,这是我的配置类,拿起关于此的信息并骑我的conectionString,这个数据我保存在加密文件中,我正在使用session-每个请求,然后在安装之前我需要检查会话配置文件,如果没有我抛出异常。

 protected void Application_BeginRequest()
 {
    if (!Settings.Data.Valid())
       throw new SingletonException();

     var session = SessionManager.SessionFactory.OpenSession();
     if (!session.Transaction.IsActive)
        session.BeginTransaction(IsolationLevel.ReadCommitted);

     CurrentSessionContext.Bind(session);
 }

除非我必须重定向到设置页面,这是一个单例类。

protected void Application_Error(Object sender, EventArgs e)
{
    Exception exc = Server.GetLastError();
    while (exc != null)
    {
        if (exc.GetType() == typeof(SingletonException))
        {
            Response.Redirect(@"~/Settings/Index");
        }

        exc = exc.InnerException;
    }
}

但是我遇到了这个重定向的问题,浏览器中的链接正在改变,但我有一个重定向循环,已经尝试清除cookie并启用外部站点的选项。 enter image description here 有人能帮助我吗?

2 个答案:

答案 0 :(得分:2)

问题是您正在使用while循环,因此如果exc不是null,则它是无限循环,您必须在此处使用if条件:

if(exc != null)
{
  if (exc.GetType() == typeof(SingletonException))
  {
      Response.Redirect(@"~/Settings/Index");
  }

  exc = exc.InnerException;
}

答案 1 :(得分:1)

设置Application_BeginRequest,以便在无效时不做任何事情。

 protected void Application_BeginRequest()
        {
            if (!Settings.Data.Valid())
                return;

            var session = SessionManager.SessionFactory.OpenSession();
            if (!session.Transaction.IsActive)
                session.BeginTransaction(IsolationLevel.ReadCommitted);
            CurrentSessionContext.Bind(session);
        }
相关问题