Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official ASP.NET Blog

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


ASP Insider Follow Me On Twitter
Properly Using Custom Error Pages in ASP.NET

Earlier this week I started with a rant about ASP.NET and Search Engine Optimization and was really tired of my platform being dissed as a good platform for optimized sites. One of the complaints raised by the article was the way ASP.NET handles custom errors. The complaint centers around two status codes being generated, first a 302 or temporary redirect is sent. Then a 200 status code is sent for a successful request. This is actually the proper way to indicate the custom error page process and I will explain why.

The basic premise behind custom error pages in ASP.NET is you as the developer can designate a page to be rendered when an unhandled exception is thrown by a request. You can also designate specific or specialized error pages be displayed when a specific status code, 404 for example is raised. The 404 status code is served when a resource cannot be found by the server.

How to Designate a Custom Error Page

The first thing I do when defining a custom error page is add the page to the site. The error page generally will consist of a notice to the user that something when wrong and a little reassurance that we (meaning I) will fix it as soon as we can. This is configured in the web.config file in the customErrors section. This element has two attributes, defaultRedirect and mode. The defaultRedirect attribute specifies the page that is displayed when an unhandled exception bubbles up through the chain of events.

<customErrors defaultRedirect="~/error.aspx" mode="RemoteOnly"/>

The mode attribute can be set to either On, Off or RemoteOnly. When set to On the custom error page is displayed both to remote computers and the local server computer. When it is set to Off no custom error page is displayed and the actual exception details a spewed onto the page for any visitor creating the exception. This is known as the Yellow Screen of Death because the text has a yellow background.

Yello Screen of Death

Finally RemoteOnly will only show the custom error page to remote users, but the actual exception messages to the local computer. This is very useful when you have access to the web server and are trying to track down a pesky bug.

Designating Custom Pages for Status Code Errors

One of the lesser known tricks with custom ASP.NET error pages is the ability to designate custom pages to be sent to the browser when specific status codes are generated. For example when a 404 or page not found status is generated this is an error. A 404 status code is sent in the response to the browser. In IIS you can designate a specific resource to be sent to the client or a generic 404 error will be displayed by the Browser.

ASP.NET allows you to designate custom pages for each status code. A status code is used to tell the web client about the request. A code of 200 means successful, 301 means permanently redirected, 404 means not found and 500 means there was an internal server error.

To add custom error pages for various status code you can add those to the customErrors element like this:

<customErrors defaultRedirect="~/error.aspx" mode="RemoteOnly">

<error statusCode="403" redirect="default.aspx"/>

<error statusCode="404" redirect="default.aspx"/>

</customErrors>

Notice how I redirect the request to the home page for a page not found status. I do this as a catch all because I inherit many sites where the owners are not certain of what they actually have, or there are so many possible pages that it is a last ditched measure to get the visitor to something important on the site. If you set up your URL Rewriting engine properly you should be able to stop any of these types of things from happening.

Custom Error Handling in ASP.NET and Status Codes

So this leaves the final issue, the status codes that are sent up when a custom error page is generated in ASP.NET. The issues {insert name here} was having is a 302 status code is sent, followed by a 200 status code. Which is exactly how it should work! The 302 status code is a temporary redirect, so we are telling the client that for now this is where this page is going, but it should come back soon.

Yes we have an error, which should be handled very soon and this page will return to this location as expected, so please do not remove me from your index, cache or what have you.

Next a custom error page is successfully returned, hence the 200 or successful status code sent to the client. I keep saying client instead of browser because the real issue for Search Engine Optimization is what the search engine spiders actually process. In this case we tell them, oops I have an error so for the time being go here. That page is successfully processed and returned, just like we wanted.

So in summary, configuring custom error pages in ASP.NET is a pretty trivial task. The status codes generated by the custom error page process are correct and as expected. Once the error is corrected the search engines should revisit the page and get the proper content from the page, so a temporary redirect or 302 status code is the correct code to return.

 

kick it on DotNetKicks.com
Share this post :
Posted: Sunday, September 30, 2007 1:12 AM

by Chris Love

Comments

Rich said:

A status code 200 certainly does not seem appropriate for when a page is no longer found.  For example, let's say you have several products that you sell off products.aspx?id=[product id].  Those products are indexed by Google.  Now, you take a product offline and want Google to remove that page from it's index, so you have your code set up to throw a HttpException with a 404 error code if a product cannot be found.  

Using the method you described, ASP.NET will redirect you to your 404 error page, so the user will see what you want them to see.  However, Google would not delete that item from it's cache, since it never gets a 404 response code.

# October 1, 2007 10:49 AM

Chris Love said:

The case where you remove a product you would want to either 301 that URL or maybe change the page to indicate to the user the product is no longer available. This would require more customized logic and an httpModule to properly do in my opinion or at least that is the track I would take on this. No it should not be a 404 code. My opinion in this case is take any indexing you can get from the search engines as long as you can get it. This will generate traffic on your site and you can then direct the visitor to what you want them to see.

This article shows how to handle legitimate errors, such as exceptions. In the case of 404 situations on my sites at least, I have found this to be URLs that really do not exist and I am not sure how the visitor found or created that URL. It also helps to fight hackers by sending them just to the home page in this case.

# October 1, 2007 12:57 PM

Structure Too Big said:

Chris Love has a good post about error handling in ASP.NET. There's a lot of debate (and perhaps confusion) about properly implementing error handling and URL rewriting (see this post) but Chris has some good insight on this.

# October 2, 2007 1:04 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 2, 2007 3:16 PM

Pedro said:

I agree with Chris that this is good for Error Pages. but like Rich said, it is not good for pages that are not found (404).
# November 13, 2008 1:03 AM

Jon said:

A real 404 should throw a 404, not a 301 or a 302, in my book. Your observation that any search engine traffic is good regardless of whether the page should be indexed anymore is shortsighted. First, it can clutter up your search results for the pages you actually want to promote. Second, it can lead to your site losing index relevance if the bulk of your links are for pages that shouldn't exist anymore.
# November 18, 2008 12:27 PM

Hydrocodone apap. said:

Hydrocodone without prescription. Hydrocodone. Difference between hydrocodone and morphine. Side effects of hydrocodone. Hydrocodone and online and prescriptions.

# May 31, 2009 9:04 PM

Jonathan said:

I agree with Jon. A real 404 should return a 404. The search engines are actually analyzing/classifying 200/301/302 pages to detect 404 pages that are not returning the appropriate status code. You're not doing anyone a favor by returning anything else... The standard exists for a reason.
# August 13, 2009 2:59 PM

Nimesh – Perception System said:

Nice Post Informative and useful one I am .Net Developer and I am looking for this Thanks for the great stuff.
# August 29, 2009 1:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS