Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 805
Mostafa Avatar asked Dec 20 '10 08:12

Mostafa


People also ask

In which file the customErrors is specified?

config settings, customErrors can be configured within the Machine. config, root web. config or your application's web. config file.

How do I change the default error page in web config?

The <customErrors> section in Web. config has two attributes that affect what error page is shown: defaultRedirect and mode . The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD.

What is customErrors tag within a web config?

When customErrors is set to On or RemoteOnly, you need to specify the defaultRedirect attribute. This attribute contains the error page to which the user will be redirected. Additionally you can take custom error handling a step further by associating specific errors with specific error pages.


2 Answers

That's interesting, After couple of years I suddenly figure it out what's the problem.

Thanks to @David solution, But the reason and complete solution is as bellow :

By setting customErrors mode to "On" it's only works when we get an exception in ASP.NET application, While when we trying to reach nonExistingdirectory Or notExsitingStaticResouce, IIS render 404 error and it does not reach to asp.net runtime and serverd by IIS directly.

So we need to add configuraiton for IIS as bellow in Web.config:

  <system.webServer>
    <httpErrors errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="~/404.html" responseMode="File" />
    </httpErrors>
  <system.webServer>

It's important to set responseMode to "File", Otherwise status code automatically would change from 404 to 200 . So from the client perspective they don't get the actual 404 status code.

like image 154
Mostafa Avatar answered Nov 16 '22 02:11

Mostafa


@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>
like image 22
David Avatar answered Nov 16 '22 03:11

David