Search code examples
c#http-redirectmodel-view-controllersession-per-request

Error when redirect page from Application_Error


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?


Solution

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