Question is: Why is custom Error handling not working for non-existing paths/directories?
Updated with fixed code (thanks to everyone for you input):
ASP.NET is never being invoked by IIS. IIS handles the page request, sees that the page doesn't exist, and ASP.NET never gets loaded.
If you want ASP.NET to get loaded regardless of whether the file exists, you need to change your IIS configuration. Are you using IIS6 or IIS7/7.5?
You need to set up wildcard mapping so that all requests go through .Net
if your using IIS6 the following link should help you out:
http://professionalaspnet.com/archive/2007/07/27/Configure-IIS-for-Wildcard-Extensions-in-ASP.NET.aspx
Wildcard script mapping and IIS 7 integrated pipeline:
http://learn.iis.net/page.aspx/508/wildcard-script-mapping-and-iis-7-integrated-pipeline/
From this comment to Mr. Disappointment:
Thanks, I'm using IIS 7 local and IIS 7.5 on live. Let me know when you find the material.
If your application is running in an application pool configured to run in Classic Pipeline mode then content not intended for ASP.NET won't hit the ASP.NET runtime. i.e. folders that don't exist. These will be handled directly by IIS.
You have a couple choices:
Set the application pool to Integrated Pipeline mode. You may also need to configure the following setting if IIS's error handling "eats" your ASP.NET 404 and 500 status code from ASP.NET:
<configuration>
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
If the application is not well behaved in "Integrated Pipeline" mode but you're just concerned about getting a page displayed for a 404 response then configure the following:
<system.webServer>
<httpErrors>
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath=""
path="/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
You'd need to set the 404
status code in the page, otherwise it'll just return a 200
.
If you use a static page, for example:
<error statusCode="404" prefixLanguageFilePath=""
path="404.html" responseMode="File" />
This will return a 404
.
If the application is not well behaved in "Integrated Pipeline" mode AND you absolutely must pass 404 errors through your error handler then you may have to manually map wildcard content to the ASP.NET HttpHandler so that such requests do hit the pipeline. This would be a sub-optimal solution.
I can't find the source material for this but I believe the problem lies in the fact it is handling custom error pages and not paths.
You investigation suggests you came across this, inasmuch as:
www.mysite.com/nonexistingpath/nonexistingpage.aspx
This ought to hit the correct error page. The following won't:
www.mysite.com/nonexistingpath/
This kind of reiterates you already answering your own question, but I'll see if I can find the reference material. Ultimately, it isn't a page request, so there is no ISAPI handling through appropriate handlers.