Tuesday, September 18, 2007

Application_Error, Global.asax : Exception Handling

When I tried manage exceptions in Application_Error event, sometimes throw an error:

Cannot redirect after http headers have been sent

My trick for avoid this exception:

void Application_Error(object sender, EventArgs e)
{
// handle generic application errors
Exception ex = Server.GetLastError();
SiteHelper.HandleExceptionInLog(ex);
Server.ClearError();
Response.Clear();
Server.Execute("Error.aspx");
}

I read these articles before:
http://blogs.msdn.com/kaevans/archive/2003/07/07/9791.aspx
http://www.codeguru.com/csharp/.net/net_asp/miscellaneous/article.php/c12385/
http://forums.asp.net/t/44586.aspx

It works for me.

No comments:

Google