IIS and ASP.NET 404 error handlers don't play nicely together

While trying to setup both ASP.NET as well as IIS error-handling, I noticed that IIS wasn’t dealing with certain 404 errors in which the file extension was wrong.  For example, I could request ThisFileDoesntExist.aspx and that would redirect to NotFound.aspx just fine, but WrongExtension.aspz would kick the user out to the ugly default IIS 404 error page.

So I did a bit of investigation and found this post, which pointed out that, in your web.config, if you have both this (relevant parts highlighted):

<customErrors defaultRedirect="~/Error.aspx” mode="On” redirectMode="ResponseRedirect”>
    <error statusCode="404redirect="~/NotFound.aspx” />
</customErrors>


As well as this:

<httpErrors errorMode="Custom” existingResponse="Replace”>
    <remove statusCode="404” subStatusCode=”-1” />
    <error statusCode="404” prefixLanguageFilePath=”” path=”/NotFound.aspx” responseMode="ExecuteURL” />
</httpErrors>


For some reason beyond my knowledge the IIS error handler just doesn’t fire, somehow the ASP.NET 404 handling hijacks the process but doesn’t get called in the event of unknown file extensions.  However, removing the 404 element from the <customErrors> section lets the IIS error handling deal with every case, and all is well in my website once again.  It makes me wonder why the customErrors section even lets you deal with 404s if it doesn’t work as expected?  But that’s probably more of a gap in my knowledge than it is an issue with IIS/ASP.NET.  Have more insight into what’s going on? Let me know!