Error when redirect page from Application_Error

207 Views Asked by At

I'm trying to do a redirect, I have a singleton class that is my configuration class, picked up the information on this and ride my conectionString, this data I saved in an encrypted file, I am using session-per-request, then before mounting the my need to check for the session configurations file if there is not I throw the exception.

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

If there except I must redirect to the Settings page which is a singleton 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;
    }
}

However I am having a problem with this redirection, the link in the browser is changing but I'm having a redirect loop, already tried clearing the cookie and enable the option for external sites. enter image description here Can someone help me?

2

There are 2 best solutions below

0
On BEST ANSWER

just set the Application_BeginRequest for dont make nothing when is not valid.

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

The problem is you are using while loop so it is infinite loop if exc is not null, you have to use if condition here:

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

  exc = exc.InnerException;
}