Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

HTTP error 404 custom page and ASP.NET

Modified: 2008/08/20 14:36 by vanthoog - Categorized as: ASP.NET
When a user requests a non-existing page of your website, IIS will return HTTP error 404. And as a result the internet browser of the user will display an error message. The error message displayed generally doesn’t look very nice and therefore it is sometimes useful to make your own 404-error message.

The steps for making your own 404-error message are fairly simple:
- Make a html-file (for example error404.htm) containing your custom 404-error message and put it somewhere in your website (for example in the root).
- Start the IIS manager, open the properties of your website, go to the tab “Custom Errors” and select the error “404”.
IIS Custom Errors

IIS Custom Errors


- Click on “Edit”.
IIS Edit Custom Error

IIS Edit Custom Error


- Change the path of the file to the full path of your custom 404-error file (for example “d:\www\yourwebsite\ error404.htm”)

Now if you start your favorite browser and request a non-existing htm-page (for example http://yourwebsite/doesnotexist.htm) you will see the contents of your custom 404-error file. However if you request a non-existing aspx-page you will not see the contents of your custom 404-error file. Instead the following error will be displayed:

Server Error in '/' Application. 

The resource cannot be found. 
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have 
been removed, had its name changed, or is temporarily unavailable.  Please review the following 
URL and make sure that it is spelled correctly. 

Requested URL: /doesnotexist.aspx

Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

This is caused by the fact that in this case the HTTP 404 error is not triggered by IIS but by aspnet_isapi.dll (i.e. the ASP.NET handler). If you also want to use your custom 404-error file for aspx pages (or other extensions which are handled by aspnet_isapi.dll), you will have to change the web.config. Just add a <customErrors> section within the <system.web> section:

<customErrors mode="On">
    <error statusCode="404" redirect="error404.htm" />
</customErrors>

 © Evident Interactive BV