0

I am unable to use Redirect or Response.Write in my application when either the Application_End or Application_Error events occur.

protected void Application_Error(object sender, EventArgs e)
{
    string strError;
    strError = Server.GetLastError().ToString();

    this.Context.ClearError();

    Response.Write("Application_Error" + "<br/>");
    Response.Write("Error Msg: " + strError + "<br/>");
}

Any help would be much appreciated, thanks

Edited: I am trying to detect application pool recycle and redirect to an error page.

David Tansey
  • 5,813
  • 4
  • 35
  • 51
Mohsin Alam
  • 90
  • 1
  • 1
  • 8
  • this.Context seems to be null here. – Mohsin Alam Jan 31 '13 at 06:05
  • what happen then if there's an error? did you set break point to Application_Error? – Jobert Enamno Jan 31 '13 at 06:19
  • yea, added the breakpoint, the exception is not handled, it just keeps everything as is, but crashes on next server call from front end. – Mohsin Alam Jan 31 '13 at 06:27
  • why is it i put int.Parse("test"); on the Page_Load event then error has occurred and it hit my Application_Error event? – Jobert Enamno Jan 31 '13 at 06:33
  • well in my scenario, our application pool is being recycled, that is not being handled hence the application_error is fired, i'm not sure about int.Parse() scenario – Mohsin Alam Jan 31 '13 at 06:39
  • I mean the int.Parse("test") will throw an error. I intentionally put it so that an error will occur and it will execute the Application_Error. Why you have mentioned Application Pool? Meaning your site is running in IIS? You also said you put break point but the application did not hit it? You cannot set break point when you're not running locally – Jobert Enamno Jan 31 '13 at 06:47
  • i'm debugging on IIS deployed application, the cause of error is due to the unexpected application pool recycle, hence testing the application by manually refreshing the pool, the breakpoint does get hit, only problem is that i need to either open an error page on application_end or application_error, when these events occurs – Mohsin Alam Jan 31 '13 at 07:12

1 Answers1

1

You should be able to do Server.Transfer which is almost equivalent to Response.Redirect. It's understandable that you can't do Response.Write since you are no longer executing within the context of the Page.

Icarus
  • 63,293
  • 14
  • 100
  • 115
  • Server.Transfer is'nt working, does nothing in Application_End and throws an NullException in Application_Error – Mohsin Alam Jan 31 '13 at 07:04
  • Did you try clearing the Response before doing Server.Transfer? Response.Clear(); Server.Transfer(); – Icarus Jan 31 '13 at 12:33