为什么在web.config中设置customErrors在这种情况下不起作用?

时间:2021-09-17 12:19:01

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

在我在共享主机提供程序中发布的ASP.NET 3.5网站中,我已经配置了我的web.config文件,如下所示:

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

如果用户请求不存在的页面(如“www.example.com/NotExistPage.aspx”),则用户将被重定向到我们期望的FileNotFound.htm页面。

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

但是,如果用户请求某些地址,例如:“www.example.com/NotExistDirectory”而没有.aspx扩展名,则用户将遇到IIS 7.5错误页面:

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

HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。

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 .

我想知道在webconfig中设置customeError不支持这种类型的地址吗?如何防止用户看到此黄页。

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

编辑:感谢大卫的回答,但我找到了实际的原因和正确的解决方案。请看我的答案。

6 个解决方案

#1


10  

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

@Mostafa:我遇到了同样的问题。我发现可以通过在web.config文件中添加以下内容来解决:

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

#2


1  

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.

这是因为ASP.Net模块配置为处理某些文件扩展名。 IIS确定.aspx必须由ASP.Net模块处理,然后web.config中的customerrors部分(实际上是web.config本身)启动。

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

由于您已经请求了一个甚至没有为ASP.Net配置的页面,因此IIS自己处理它而不传递请求。

#3


1  

i did some search

我做了一些搜索

try this link

试试这个链接

Strange 404 error on IIS 7.5

IIS 7.5上出现奇怪的404错误

#4


1  

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 :

感谢@David解决方案,但原因和完整的解决方案如下:

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.

通过设置的customErrors模式为“开”,当我们在ASP.NET应用程序中获得一个异常的才能正常运行,而当我们试图达到nonExistingdirectory或者notExsitingStaticResouce,IIS显示404错误,它不通过IIS达到asp.net运行时服务器D和直。

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

所以我们需要在Web.config中为IIS添加configuraiton:

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

将responseMode设置为“File”非常重要,否则状态代码会自动从404更改为200。因此,从客户端的角度来看,他们没有获得实际的404状态代码。

#5


0  

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

对于.aspx以外的任何其他文件,您可以在IIS中进行配置:http://www.xefteri.com/articles/show.cfm?id = 11

#6


0  

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.

首先,目录url必须具有尾部斜杠,否则它只是一个无扩展名的文件。 www.mysite.com/NotExistDirectory/其次,ASP.net IIS模块只是ASP MIME类型的处理程序,因此目录是Web服务器的剩余部分。第三,customerror是system.web的一部分,是ASP.net配置的一部分,httperror是system.webserver的一部分,是IIS配置的一部分。假设IIS配置中的http模块默认值,httperror将对不存在的目录使用自定义错误。

#1


10  

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

@Mostafa:我遇到了同样的问题。我发现可以通过在web.config文件中添加以下内容来解决:

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

#2


1  

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.

这是因为ASP.Net模块配置为处理某些文件扩展名。 IIS确定.aspx必须由ASP.Net模块处理,然后web.config中的customerrors部分(实际上是web.config本身)启动。

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

由于您已经请求了一个甚至没有为ASP.Net配置的页面,因此IIS自己处理它而不传递请求。

#3


1  

i did some search

我做了一些搜索

try this link

试试这个链接

Strange 404 error on IIS 7.5

IIS 7.5上出现奇怪的404错误

#4


1  

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 :

感谢@David解决方案,但原因和完整的解决方案如下:

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.

通过设置的customErrors模式为“开”,当我们在ASP.NET应用程序中获得一个异常的才能正常运行,而当我们试图达到nonExistingdirectory或者notExsitingStaticResouce,IIS显示404错误,它不通过IIS达到asp.net运行时服务器D和直。

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

所以我们需要在Web.config中为IIS添加configuraiton:

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

将responseMode设置为“File”非常重要,否则状态代码会自动从404更改为200。因此,从客户端的角度来看,他们没有获得实际的404状态代码。

#5


0  

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

对于.aspx以外的任何其他文件,您可以在IIS中进行配置:http://www.xefteri.com/articles/show.cfm?id = 11

#6


0  

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.

首先,目录url必须具有尾部斜杠,否则它只是一个无扩展名的文件。 www.mysite.com/NotExistDirectory/其次,ASP.net IIS模块只是ASP MIME类型的处理程序,因此目录是Web服务器的剩余部分。第三,customerror是system.web的一部分,是ASP.net配置的一部分,httperror是system.webserver的一部分,是IIS配置的一部分。假设IIS配置中的http模块默认值,httperror将对不存在的目录使用自定义错误。