从 Application_Error 重定向页面时出错

Error when redirect page from Application_Error

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

 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);
 }

如果有除了我必须重定向到单例的设置页面 class。

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;
    }
}

但是我在使用此重定向时遇到问题,浏览器中的 link 正在更改,但我有一个重定向循环,已经尝试清除 cookie 并启用外部站点的选项。 有人可以帮助我吗?

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

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

  exc = exc.InnerException;
}

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