Why setting customErrors in web.config doesn't work at this case?

Mostafa picture Mostafa · Dec 20, 2010 · Viewed 15k times · Source

In my ASP.NET 3.5 Website which is published in shared hosting provider , I've configured my web.config file like this :

    <customErrors mode="On" defaultRedirect="GenericErrorPage.htm">
        <error statusCode="403" redirect="AccessDenied.htm"/>
        <error statusCode="404" redirect="FileNotFound.htm"/>
    </customErrors>

If the user request pages that doesn't exist ( like "www.example.com/NotExistPage.aspx" ) , user will be redirected to FileNotFound.htm page as we expect .

But if the user request some address like : "www.example.com/NotExistDirectory" without .aspx extension , the user will encounter IIS 7.5 Error page :

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

Detialed error information :

Module  IIS Web Core
Notification    MapRequestHandler
Handler StaticFile
Error Code  0x80070002

Requested URL   http://www.example.com:80/NotExistDirectory
Physical Path   D:\Websites\example\example.com\wwwroot\NotExistDirectory
Logon Method    Anonymous
Logon User  Anonymous

This is a yellow page which is not user friendly and we didn't expect .

I'm wondering setting customeError in webconfig doesn't support this type of address or not ? How can i prevent users seeing this yellow page .

Edit : Thanks to David's answer , But I found the actual reason and correct solution. Please see my answer.

Answer

David picture David · Feb 1, 2012

@Mostafa: I faced the exact same problem. I found out it can be solved by adding the following to the web.config file:

<system.webServer>
    <httpErrors errorMode="Custom">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" subStatusCode="-1" prefixLanguageFilePath="" path="/MyErrorPage.aspx" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>