16

I don't know why the Response.Redirect not working properly when I deploy my code to IIS7? The white/yellow error page always get displayed instead of my Errors.aspx. But when debug running using Visual Studio on my computer, it runs just fine?

protected void Application_Error(object sender, EventArgs e)
        {
            ILog log = LogManager.GetLogger(typeof(Global).Name);
            Exception objErr = Server.GetLastError().GetBaseException();
            log.Error(objErr);
            string err = "Error Caught in Application_Error event\n" +
                    "\nError Message:" + objErr.Message.ToString() +
                    "\nStack Trace:" + objErr.StackTrace.ToString();
            EventLog.WriteEntry("Kiosk", err, EventLogEntryType.Error);
            Server.ClearError();
            Response.Redirect("~/Error.aspx", false);
        }
Leo
  • 2,173
  • 6
  • 28
  • 37
  • if you attach a debugger, can you break into the code at see if Response.Redirect is actually being called? – Pauli Østerø Dec 14 '10 at 04:30
  • well, I debugged using Visual Studio on my machine and it runs fine. But when deploying to IIS, it doesn't run anymore – Leo Dec 14 '10 at 04:46

5 Answers5

31

I had the same problem and solved it with:

HttpContext.Current.ClearError();             
Response.Redirect("~/Error.aspx", false);
return;
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Christian van R
  • 617
  • 5
  • 6
4
HttpContext.Current.Server.ClearError();
HttpContext.Current.ClearError();
====================================================================
Redirect to NEW VIRTUAL! directory (Error)
HttpContext.Current.Response.Redirect([http://localhost:8990/Error/ErrorPageServer.aspx]);
jeka
  • 51
  • 1
4

For me the below code worked.

HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Redirect("~/ErrorPage.aspx");
Ankit
  • 760
  • 6
  • 15
1
protected void Application_Error(object sender, EventArgs e)
{            
     Exception objErr = Server.GetLastError().InnerException;
    //Logging.WriteToErrorLog("Error Caught in Application_Error event", objErr);
   HttpContext.Current.Server.ClearError();
   HttpContext.Current.Application.Add("test", objErr);
   HttpContext.Current.Response.Redirect("~/Home/Index");
   return;
}
0

Try to turn off the CustomError in web.config. It will give you more specific about the error details. Maybe it doesn't the error from Response.Redirect.

Tee Wu
  • 569
  • 2
  • 9
  • yes I tried that already, I know what error that lead to this Application_Error to be called and it was logged properly by the above logging codes but it still not redirect to Error.aspx :( – Leo Dec 14 '10 at 04:10