IIS7自定义404没有显示

rob*_*rob 41 .net iis-7 http-status-code-404

使用Intergrated .net 4.0应用程序池创建了一个新的IIS7网站.

以.aspx结尾的网址会显示自定义404其他任何内容都会显示蓝色服务器错误页面"HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用." (所以与IE无关)

<customErrors redirectMode="ResponseRewrite" mode="On" defaultRedirect="/pages/404.aspx" />
</system.web>
<system.webServer>
    <httpErrors  >
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx" responseMode="ExecuteURL" />
    </httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

也试过了

<httpErrors existingResponse="PassThrough" />
Run Code Online (Sandbox Code Playgroud)

但这只是导致空洞的回应.

我只找到了一个对执行appcmd来测试自定义http错误处理的有用性的引用,但这里是结果.

C:\Windows\System32\inetsrv>appcmd list config "http://mysite/file.notexist" -section:httpErrors

<system.webServer>
    <httpErrors>
        <error statusCode="401" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="401.htm" />
        <error statusCode="403" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="403.htm" />
        <error statusCode="404" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="404.htm" />
        <error statusCode="405" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="405.htm" />
        <error statusCode="406" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="406.htm" />
        <error statusCode="412" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="412.htm" />
        <error statusCode="500" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="500.htm" />
        <error statusCode="501" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="501.htm" />
        <error statusCode="502" prefixLanguageFilePath="%SystemDrive%\inetpub\custer
r" path="502.htm" />
    </httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

这是奇怪的,因为在iis7管理器中错误页面显示

404 /pages/404.aspx Execute URL Local
Run Code Online (Sandbox Code Playgroud)

.Net错误页面没有显示任何内容,尽管我确实有一个条目.

问题1:我需要采取哪些步骤才能为全新的.net 4 iis7网站提供每404结果的自定义.net错误页面?

问题2:为什么.net处理程序适用于.aspx文件而没有其他内容?

注意:在服务器级别设置404,然后appcmd命令在路径中显示自定义404,但对于无法显示404的站点没有任何区别.

所以我猜这是一个红鲱鱼.

rob*_*rob 110

回答是使用

    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx?he" responseMode="ExecuteURL" />
    </httpErrors>
Run Code Online (Sandbox Code Playgroud)

并且没有任何system.web customErrors

这适用于.aspx和非.aspx请求.

奇怪的是,这个组合没有出现在我调查的任何博客文章和stackoverflow答案中,只是运气我试过了.

  • 希望我可以额外多次投票.感谢您的帮助!...另外值得一提的是,我从这篇文章中注意到,"路径"部分不应该以"〜"开头 (5认同)
  • 修复我的问题的关键项是将`existingResponse ="Replace"errorMode ="Custom"`属性添加到`httpErrors`节点. (5认同)

mhe*_*384 6

<httpErrors existingResponse="PassThrough" />
Run Code Online (Sandbox Code Playgroud)

在IIS 7(Windows Server 2008 R2)中为我工作.

我的问题是我在我的web.config中有这个,但在一个<location>.将它移动到根<system.webServer>修复它.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    ...
    <system.webServer>
        ...
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)