This blog post will explain how to configure custom error pages in IIS (Internet Information Server). For this example we will be using IIS 7.5 which shipped with Windows Server 2008 R2.
If we configure .NET Error Pages at the site level, ASP.NET stores the settings in the site’s web.config file. Since these settings are stored in the web.config file they are portable and can be easily moved to another server with the site’s content.
How to setup Custom Error Pages in IIS 7.5
Open Internet Information Services (IIS) Manager. Select your website. Note: This could also be set at the server level and applied to all sites on the server. DoubleClick on the “.NET Error Pages” icon.
The .NET Error Pages features view will be displayed.
Click the “Edit Feature Settings” link to enable this feature. The “Edit Error Page Settings” dialog box will appear.
In order to change the default mode, we must also specify a “Default Page”. This page will be used for all status codes that are not otherwise defined. In our example we are using a generic custom error page to trap all other errors. Once you enter the absolute URL for the default error page click OK. Note: It may be a good idea to use a static HTML page here just in case ASP.NET is not functioning properly.
By default server errors are shown when logged on locally to the IIS server and custom errors will only be used from remote sessions. We will want to change this to “On” if we are logged on locally to the IIS server. Otherwise, it will display detailed server errors, and not our custom error pages.
Next we will explicitly define the 404 Error code.
To get the browser to throw a 404 error, we pointed it to a file on the test site that does not exist. As you can see in the following image the friendly HTTP 404 error page was shown in IE9.
On the .NET Error Pages Actions menu click the Add link.
The “Add Custom Error Page” dialog will appear. This is where we define individual error pages per status code. For our example we will add a custom page for the HTTP 404 Error.
Now that we have turned on the feature and added a custom page for the 404 status code we can verify it is working. To verify visit a page that does not exist. In our example we will use http://mysite.com/deletedfile.aspx. You can see in the following image that the custom error page was shown.
Our custom 404 Error message in IE 9:
As mentioned above this can also be managed from the site’s web.config file. Consider the following configuration section from our site’s web.config file.
<customErrors defaultRedirect=”http://mysite.com/errors/Error.aspx” mode=”RemoteOnly”>
<error redirect=”http://mysite.com/errors/404.aspx” statusCode=”404″ />
Everything we set in the GUI can easily be set directly in the web.config. This will also allow you to setup .NET Error Pages, if you are on a shared hosting Plan. Here at SherWeb, our shared, dedicated and Windows cloud server hosting plans can all benefit from using custom .NET Error Pages.