Reputation: 1491
I have the following in my Global.asax
which is meant for handling errors:
void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
if (exception != null)
{
//Log
if (HttpContext.Current.Server != null)
{
HttpContext.Current.Server.Transfer("/siteerror.aspx");
}
}
}
This works for the most part, but sometimes does not get into Server.Transfer
. For some reason HttpContext.Current.Server
is null. I figured out where this happens: when errors occur in a user control and in my business logic classes.
Am I missing something here?
Upvotes: 14
Views: 32808
Reputation: 7438
Application_Error block can catch exception anytime between application life cycle.
Application life cycle is parent of Session life cycle as you can understand there can be many sessions within a single application.
Thus you may have HttpContext.Current null at certain errors occured before creating session or after expiring of sessions.
If you want to write session specific error redirects you should check for Null of current HttpContext always.
You can also use Server.GetLastError to know the error details occured and write your error page redirect through CustomError tag in web.config
See the following link
Upvotes: 2
Reputation:
Application Errors can occur at any time - including when no connection is present.
For example, if a background thread is processing and causes an exception.
Upvotes: 1