Sunday, October 10, 2010

.NET Fundamentals 12 (ASP page Error handler)

ASP.NET provides several levels at which you can handle and respond to errors that may occur when you run an ASP.NET application. ASP.NET provides three main methods that allow you to trap and respond to errors when they occur: Page_Error, Application_Error, and the application configuration file (Web.config).

Page_Error:
The Page_Error event handler provides a way to trap errors that occur at the page level. You can simply display error information or you can log the event or perform some other action.
void Page_Load(object sender, System.EventArgs e)
{
    throw(new Exception());
}


public void Page_Error(object sender,EventArgs e)
{
    Exception objErr = Server.GetLastError().GetBaseException();
    Server.ClearError();
}
Server.ClearError prevents the error from continuing to the Application_Error event handler.

Application_Error:
The Application_Error event handler is specified in the Global.asax file of application. It means other pages in same virtual directory can also use the same error handler.
void Page_Load(object sender, System.EventArgs e)
{
    throw(new Exception());
}
//In Global.asax
 protected void Application_Error(object sender, EventArgs e)
{
    Exception objErr = Server.GetLastError().GetBaseException();
    Server.ClearError();  
}

Note:
1) In both of the above methods AutoEventWireup in aspx page should be true. It is ASP attribute which wires up certain event handlers (like Page_Load, Page_Error etc) to the page.If this attribute is set to false then none of these events will fire. These event handler have fixed name which can't be changed.
2) The error details can be found in Server.GetLastError().GetBaseException().

Web.Config method:
If you do not call Server.ClearError or trap the error in the Page_Error or Application_Error event handler, the error is handled based on the settings in the section of the Web.config file. In the section, you can specify a redirect page as a default error page (defaultRedirect) or specify to a particular page based on the HTTP error code that is raised. You can use this method to customize the error message that the user receives.
Change the settings in customeErrors tag in web.config file.

mode = On
DefaultRedirect = Page where you want to redirect on error.
Inside customeErrors tag you have en error tag in which you can specify different page for different errors.The error tag looks like this:
error statusCode="404" redirect="filenotfound.htm" 
Status code is error code and the redirect will be the page where w want to redirect.


In customErrors we can select mode attribute. It hase 3 values to set:
-> On: Unhandled exceptions redirect the user to the specified defaultRedirect page. This mode is used mainly in production.
-> Off: Users receive the exception information and are not redirected to the defaultRedirect page. This mode is used mainly in development.
-> RemoteOnly: Only users who access the site on the local computer (by using localhost) receive the exception information. All other users are redirected to the defaultRedirect page. This mode is used mainly for debugging.



Here is one more attribute which needs to be set to achive desired purpose, that is redirecmode.
It is an optional property. It has 2 values which can be set depending upon requirement.

ResponseRedirect: Specifies that the URL to direct the browser to must be different from the original Web request URL.In this case the server will completely redirect your application page to error page. All the content of application page will not be available anymore.
ResponseRewrite: Specifies that the URL to direct the browser to must be the original Web request URL.In simple words in this case the url which you see in webbrowser will be the application url only, but the http content will be of error page.Here server rewrite the html content of your application page.

The different among 2 can be seen on refresh of page. In 1st case error page will get refresh. but in 2nd Case your application will again try to reload and if again some error occurs then we will get error page again.



ApplicationInstanceError method:
Wen can use this method in following way:
HttpContext.Current.ApplicationInstance.Error += new EventHandler(ApplicationInstance_Error).
by this we can attach error handler in our application. it is equivalent to response.rewrite method in customeError tag mentioned above. If we have both Application_Error(In Global.asax) as well as ApplicationInstanceError handler then Application_Error will be called 1st and then the other one.
The benefit we get by this is, Response.ReWrite doesn't work for application in different virtual directory, but by this method we get all the benefit of response.rewrite and whoever application uses this Dll having ApplicationInstanceError handler will be able to invoke it.

No comments:

Post a Comment