Error Handling Within Your Asp.Net Web Application
As you build out your Asp.Net web application, one portion of the application that is usually overlooked is error handling. Few developers take the time to build clean code and then build adequate error handling in the event that an unhandled error is found. Error handling can be done very easily with only minimal lines of code providing the end user with a professional and well designed experience.
Step 1: Smart Code
When building out your application, the key is to stop and think about what you are doing and are there possibilities that this code could error out. Typically this is most identified when you are working with user input. In such a case, the Try-Catch-Finally statements are crucial to clean and secure coding. Using the try statyemtns around potential problem code can often gracefully catch and error at which time you can handle appropratly.
Step 2: Web.Config
From within your application, you will need to turn the custom errors on.
<customErrors mode="On" defaultRedirect="Error.aspx">
</customErrors>
The above code will cause the end user to be redirected to “Error.aspx” in the event of an unhandled error. You can also setup custom error redirection pages for any IIS specified error code. Typically 404 (Page not found) and 500 (Script error) are used but not necessary.
Step 3: Page Base
A core programming concept that is very useful to implement is a page base. This is a great place to store items such as properties to hold your session variables and other core methods that each page needs to access. This page base inherits from the page class and then each page in your application would inherit from basebase. From within your page base class, you can setup the Page_Error method which is automatically called first when an unhandled error happens. This is a great place to enter a few lines of helpful code to catch the error, along with other pertinent browser data, and email that to the developer. Here is an example of a Page_Error method:
public void Page_Error(Object sender, EventArgs e)
{
String pageException = Server.GetLastError().ToString();
StringBuilder strBuild = new StringBuilder();
strBuild.Append("Exception!");
strBuild.Append(pageException);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("---------------------------");
strBuild.Append(Convert.ToChar(13));
strBuild.Append("IP Address: " + Request.UserHostAddress);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Browser Type: " + Request.Browser.Type);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Browser Name: " + Request.Browser.Browser + " -- Version: " + Request.Browser.MajorVersion + Request.Browser.MinorVersionString);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Browser Platform: " + Request.Browser.Platform);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports Frames: " + Request.Browser.Frames);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports Tables: " + Request.Browser.Tables);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports Cookies: " + Request.Browser.Cookies);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports VB Script: " + Request.Browser.VBScript);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports Scripts: " + Request.Browser.EcmaScriptVersion);
strBuild.Append(Convert.ToChar(13));
strBuild.Append("Supports Java Applets: " + Request.Browser.JavaApplets);
strBuild.Append(Convert.ToChar(13));
Email.SendErrorEmail("Your Website Error " + DateTime.Now.ToString(), strBuild.ToString());
}
Note that to send out this email, I am calling into a static class method called “SendErrorEmail” which I have previously created for quick and efficient email sending. Click Here to view a post on sending email in your ASP.Net application.
Step 4: Global.asax
The next step that your unhandled error moves past is the Global.asax. You can utilize the the “Application_Error” method to again trap any errors that were not handled by the page base code above. And example of the globa.asax code is as follows:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
String Message = "\n\nURL:\n http://localhost/" + Request.Path
+ "\n\nMESSAGE:\n " + Server.GetLastError().Message
+ "\n\nSTACK TRACE:\n" + Server.GetLastError().StackTrace
+ "\n\nDATA:\n" + Server.GetLastError().Data
+ "\n\nSOURCE:\n" + Server.GetLastError().Source
+ "\n\nFORM:\n" + Request.Form.ToString()
+ "\n\nQUERYSTRING:\n" + Request.QueryString.ToString()
+ "\n\nTARGET SITE:\n" + Server.GetLastError().TargetSite;
Email.SendErrorEmail("SF Global Error " + DateTime.Now.ToString(), Message);
}
This second example does not include the browser data but you can easily add that in. Finally after the page base error and the global.asax error handling code are referenced, the web.config preferences are put into action. If everything is setup correctly, your end user will always see a clean “Woops” page and you will be notified with the data you need to identify and fix the error.
