开发者

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

开发者 https://www.devze.com 2023-01-31 22:22 出处:网络
In my ASP.NET 3.5 Website which is published in shared hosting provider , I\'ve configured my web.config file like this :

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" 开发者_JAVA百科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.


@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>


Thanks to @David solution, But the reason and complete solution are as below:

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

So we need to add configuration for IIS as below 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's perspective, they don't get the actual 404 status code.


This is because the ASP.Net module is configured to handle certain file extensions. IIS determines that .aspx must be handled by the ASP.Net module and then the customerrors section in the web.config ( and indeed web.config itself) kicks in.

Since you have requested a page not even configured for ASP.Net, IIS handles it on its own without passing the request on.


For any other files other than .aspx, you can configure this in IIS: http://www.xefteri.com/articles/show.cfm?id=11


First, directory url must posess trailing slash, otherwise its just an extensionless file. www.mysite.com/NotExistDirectory/
Second, ASP.net IIS module is handler for ASP MIME types only, so directory is leftover for web server. Third, customerror is part of system.web is part of ASP.net configuration
and httperror is part of system.webserver is part of IIS configuration.
Assuming http module defaults in IIS configuration httperror will work with custom error for non-existing directory.

0

精彩评论

暂无评论...
验证码 换一张
取 消