Custom Errors in IIS7

Having recently moved over to IIS7, one of my stumbling blocks has been around custom errors.
This works really well in IIS6, but as soon as you move over to IIS7 seems to completely stop working in most cases.

Requests to folders or files without .aspx extensions seems to just throw a Server Error

ServerError

This was less than ideal, so I set out to find out the right way to do this in IIS7.

You may be familiar with a section in your web configuration file named :

<system.webServer>

In IIS6 this section is totally ignored, but the minute you move to IIS7 this section becomes oh so important.
We generally setup our modules and handlers here, but there is another section we can add called:

<httpErrors>

Now this can also be controlled through the IIS7 interface, but here I'll show you how to do this in code.

Firstly, this element is locked at application server level.
This means you will not be able to modify it in your web configuration file unless you allow this in your application host configuration file.
If you’re unfamiliar with this file, you can think of it as IIS’s Application Configuration file.

Before you do this, please note:

It is highly dangerous to mess with this file and can take down every website on your entire web server. Please approach with caution!
Please back this file up before trying anything

Now that we have that out the way, you can find this file in the following location:

c:\Windows\System32\inetsrv\config\applicationHost.config

in this file navigate to a line that reads

<section name="httpErrors" overrideModeDefault="Allow" />

And alter the overrideModeDefault to be Allow

<section name="httpErrors" overrideModeDefault="Allow" />

Save this file, and you are now free to add HttpErrors to your web configuration file.
So, inside the system.webServer section add the following code

<httpErrors errorMode="Custom" existingResponse="Replace" defaultPath="/500/Default.aspx" defaultResponseMode="ExecuteURL">
  <clear />
  <error statusCode="404" path="/404/default.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
</httpErrors>

Now this is pretty much the magic that makes your 404’s (and 500’s) work properly.
You may now remove or comment out your existing customErrors section.

 

 


Burn!

posted @ Monday, September 07, 2009 8:21 AM

Print
«October»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910