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

时间:2010-12-20 08:08:01

标签: asp.net web-config iis-7.5 custom-errors

在我在共享托管服务提供商中发布的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>

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

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

  

HTTP错误404.0 - 未找到   你正在寻找的资源   删除,更改名称,或者是   暂时不可用。

已解决的错误信息:

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

这是一个非常用户友好的黄页,我们没想到。

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

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

6 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:1)

我做了一些搜索

试试此链接

Strange 404 error on IIS 7.5

答案 3 :(得分:1)

这很有意思,经过几年的努力,我突然发现了问题所在。

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

SELECT (SELECT col1 FROM table WHERE col2=1) * (SELECT col1 FROM table WHERE col2=2) 模式设置为&#34; customErrors&#34;它只在我们在ASP.NET应用程序中获得异常时起作用,当我们尝试到达OnnonExistingdirectory时,IIS呈现404错误并且它没有到达asp.net运行时由IIS直接服务。

因此我们需要在Web.config中添加IIS的configuraiton:

notExsitingStaticResouce

<system.webServer> <httpErrors errorMode="Custom"> <remove statusCode="404"/> <error statusCode="404" path="~/404.html" responseMode="File" /> </httpErrors> <system.webServer> 设置为&#34; responseMode&#34;非常重要,否则状态代码会自动从404更改为200。因此,从客户的角度来看,他们没有获得实际的404状态代码。

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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