Setting custom errors in asp.net

If you want more control over what is rendered when there’s a problem with your website, then you will need to set custom errors for any problems you foresee. Two common examples of this are “Page not found aka 404 error” and “Internal server error aka 500 errror”. The 404 err0r happens when a page that doesn’t exist is being requested while the 500 error happens when an error has not been properly trapped in your web application logic.

If you do not set custom error pages, default error pages for those errors will be displayed. There are not very nice looking albeit they convey the message to the more technical person and not the average user. So with custom error pages, you can have your own design which can be in line with your current theme or have something more sophisticated to show to the user and tell the latter what the problem is in very simple terms. However setting these custom error pages can be a problem if you do not understand how they are called.

There are 2 ways to achieve this. The first one relies on you trapping the error in your code and calling your error page. If for example, someone calls a page with an invalid PostId (a post id which does not exist in your database), you can fire off your own custom 404 page. Just remember that you need to do the following before setting the http response code to 404:

Response.TrySkipIisCustomErrors = true;

The second way to have custom error pages in asp.net is to set them in the web.config file. There’s a section under system.web for customErrors and here’s how you can use it:


<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/PageError.aspx">
 <error statusCode="404" redirect="~/PageNotFound.aspx"/>
 <error statusCode="500" redirect="~/PageError.aspx"/>
 </customErrors>

It is important to have a defaultRedirect set, otherwise IIS default error page will be shown for other statusCode that you have not defined.

You will also need to edit your web.config file and add the following to the system.webserver section:

<httpErrors errorMode="Custom">
 <remove statusCode="500" subStatusCode="-1"/>
 <error statusCode="500" path="/PageError.aspx" responseMode="ExecuteURL"/>
 <remove statusCode="404" subStatusCode="-1"/>
 <error statusCode="404" path="/PageNotFound.aspx" responseMode="ExecuteURL"/>
 </httpErrors>


customErrors vs httpErrors – What’ the difference?

For all requests which are served by asp.net, customErrors will be called. Any requests which are handled by IIS will be transferred to httpErrors. You can try it for yourself. If you have a folder which does not have a default document (eg default.aspx), then there will be an error. If you don’t have your httpErrors section defined, you won’t see your custom error page but the default IIS error page.

Remember to have the line Response.TrySkipIisCustomErrors = true in your error pages, otherwise IIS default documents might kick in.

Note that I’ve tested this on IIS7.5 and .net framework 4.0.

Google PR Update 27 July 2011

The long wait for a major Google PR update is now over as the PageRank has been refreshed today (27th July 2011). I’ve checked my sites (30+ I’ve got at the moment) and can confirm that it was not a minor update but a full scale one. Most of my sites have been an increase in PR but some of them dropped as well.

Backlinks is what PR is all about and if you have been doing your link building well, then you would be happy today to see your PR going up the scale. If you haven’t though, there’s still time although you’ll have to wait a couple of months before seeing any result if you start your link building campaign today.

What I’d like to share today is that an expired domain which I purchased a few months back, dropped to PR N/A during 2 last updates but for this PR update that just happen, it went to PR4. Well it has many nice juicy links pointing towards it and it was about time anyway. On another expired domain (which is PR3) where I linked a new site, the latter jumped from PR0 to PR3. However there’s no real movement in terms of SERPs, so I believe expired domains do not count towards ranking but they will give you PR love, so all is not lost.

And another thing which I’d like to point out (maybe reminding myself as well) is that if you stop building links (as in acquiring new links to your site, then sooner or later, you will lose your PR and possibly your traffic. This happened on one site which has been PR3 for a couple of years now but because of neglect it has fallen to PR2 and the traffic is diminishing steadily on top of that. So it looks like I’ll be busy with more promotion and link building for this one now…