IIS7自定义404没有显示

时间:2011-07-11 09:14:17

标签: .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>

也尝试了

<httpErrors existingResponse="PassThrough" />

但这只是一个空洞的回应。

我只找到了一个对执行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>

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

404 /pages/404.aspx Execute URL Local

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

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

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

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

所以我猜它是红鲱鱼。

4 个答案:

答案 0 :(得分:110)

答案是使用

    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="404" subStatusCode="-1" />
        <error statusCode="404" prefixLanguageFilePath="" path="/pages/404.aspx?he" responseMode="ExecuteURL" />
    </httpErrors>

并且没有任何system.web customErrors

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

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

答案 1 :(得分:5)

<httpErrors existingResponse="PassThrough" />

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

答案 2 :(得分:0)

对于IIS7以上,根据rob的回答,仅使用httpErrors:https://stackoverflow.com/a/6661699

添加,除非你需要/想要使用ASP.NET来呈现你的错误页面,我建议使用静态HTML文件来删除对ASP.NET的依赖。只需确保省略任何前导斜杠并在路径的其余部分使用反斜杠,例如

<error statusCode="404" prefixLanguageFilePath="" path="pages\404.html" responseMode="File" />

设置responseMode="File"以保留正确的状态代码。

答案 3 :(得分:0)

我的Web应用程序出现此问题,并通过注释掉重定向行解决了该问题:

    <httpErrors>
        <remove statusCode="403" subStatusCode="-1" />
        <!--<error statusCode="403" prefixLanguageFilePath="" path="C:\inetpub\redirectSItoHttps.htm" responseMode="File" />-->
    </httpErrors>

在IIS上,allowAbsolutePathsWhenDelegated(在配置编辑器-> system.webServer / httpErrors上)已锁定,我无法将值false更改为true。