Making Your Error Pages Pretty!

In my opinion your client/user should never see the yellow screen of death. There are numerous ways to redirect the user to a custom error page. In this post I will discuss how to use Application_Error event in the Global.asax file. Let's check out the code that will throw the error.

private string GetObjectFromSession()

{

if (Session["UserName"] == null)

throw new ArgumentNullException("UserName is null","UserName is null. Your session has expired. <res> Please log on to the system to start a new session </res>");

return Session["UserName"] as String;

}

So, the above code will throw the error since I am not storing anything in the Session Object.

You will notice that I am using the <res> ("res") tags inside for the exception message. The "res" means resolution of the problem. I think it is important to give users some information on how to solve the problem and get back on the right track.

protected void Application_Error(object sender, EventArgs e)

{

HttpContext context = HttpContext.Current;

Exception ex = context.Server.GetLastError();

Response.Write("<link href=\"Stylesheet1.css\" rel=\"stylesheet\" type=\"text/css\" />");

Response.Write(TableHelper.GenerateErrorTable(ex));

context.Server.ClearError();

}

The Application_Error event uses the TableHelper and style sheets to make the error look better or prettier. Take a look at the result shown in the screen shot shown below:

ErrorDetails2Res

Print | posted @ Thursday, October 25, 2007 2:32 PM

Twitter