Custom Error Handling in ASP.net C# (C-Sharp)

by Chris Sunday, July 19 2009

There are several methods being used to provide users with helpful 404 error pages, but just because it looks helpful to the user, doesn't mean that it is benefiting your SEO. It is essential, for the prosperity of your website, to be certain that proper server header responses are being sent.

We have taken a somewhat different approach to implementing error handling. We have included a "switch" statement in order to provide multiple errors and server header responses through one page.

Here is a sample from our "errorhandler" codebehind file:


protected override void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["m"] != null)
{
switch (Request.QueryString["m"].ToString())
{

case "404":
Response.Status = "404 Not Found";
this.Page.Title = "404 Error: File Not Found | AB Web Design";
mvError.SetActiveView(viewNotFound);
break;

case "500":
Response.Status = "500 Internal Server Error";
this.Page.Title = "500 Error: Internal Server Error | AB Web Design";
mvError.SetActiveView(viewInternalError);
break;

default:
Response.Status = "404 Not Found";
this.Page.Title = "404 Error: File Not Found | AB Web Design";
mvError.SetActiveView(viewNotFound);
break;

}
}

else
{
Response.Status = "404 Not Found";
this.Page.Title = "404 Error: File Not Found | AB Web Design";
mvError.SetActiveView(viewNotFound);

}
}

 

In the .aspx file:

 

<asp:Literal ID="ltrError" runat="server" />
<asp:MultiView ID="mvError" runat="server">

<asp:View ID="viewNotFound" runat="server">
<br />
<h1>404 Error: File Not Found</h1>
<br />
<p>We're sorry, that page was not found on this server.</p>
</asp:View>

<asp:View ID="viewInternalError" runat="server">
<br />
<h1>500 Error: Internal Server Error</h1>
<br />
<p>We're sorry, our site has experienced an error. The site administrators have been informed.</p>
</asp:View>
</asp:MultiView>

 

We have removed some of the other server header responses such as 401, 400, etc..., but as you can see, it is a simple way of covering all of your errors in one page. Plus, it will return the proper response, and prevent unwanted indexing by search engines.

We also tied in an error (exception) reporting feature using global.asax, which we will cover in an upcoming blog entry. It basically generates and sends us a detailed report email every time an error message is displayed to a user.

I hope you found this useful! If you like it, digg it!

Tags:

Microsoft | ASP.net

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



About this Blog

This is a blog about web design and all things that are related to web design. Pertinent and relevant comments and/or questions are highly encouraged, but please be courteous and thoughtful of others whilest making comments.

If you have benefited from a post, please do the right thing and promote it. You can do this by linking to it, tweeting it, or digging it.

Thank you much.

Recent Comments

Comment RSS