【问题标题】:Error when redirect page from Application_Error从 Application_Error 重定向页面时出错
【发布时间】:2015-04-16 12:25:31
【问题描述】:

我正在尝试进行重定向,我有一个作为我的配置类的单例类,获取了关于此的信息并使用了我的连接字符串,我将这些数据保存在加密文件中,我正在使用 session-per-请求,然后在安装之前我需要检查会话配置文件,如果没有我会抛出异常。

 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 并启用外部站点的选项。 有人可以帮我吗?

【问题讨论】:

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


    【解决方案1】:

    问题是您使用的是while 循环,所以如果exc 不是null,则它是无限循环,您必须在这里使用if 条件:

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

    【讨论】:

    • 虽然我得到了第一个异常,但导致第一个错误不是我的错误。 imgur.com/aPOT5pg,我删除重定向只是为了给你看,我也试过用 if 来做。
    【解决方案2】:

    只需将 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);
            }
    

    【讨论】:

      猜你喜欢
      • 2011-07-11
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多