IIS7自定义404页面未显示。

42

创建了一个新的具有集成.NET 4.0应用程序池的IIS7网站。

以.aspx结尾的URL显示自定义404,而其他任何URL都会显示蓝色服务器错误页面 "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." (所以与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:对于每个404结果,我需要采取哪些步骤才能使完全新的asp .net 4 iis7站点具有自定义的.net错误页面?

问题2:为什么.net处理程序只适用于.aspx文件而不适用于其他任何文件?

注意:将404设置为服务器级别,然后使用appcmd命令显示了路径中的自定义404,但对于未显示404的站点没有任何影响。

因此,我猜这是一个误导。

4个回答

117

答案是使用

    <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
谢谢您发布这个答案 - 这让我看起来像是知道我在做什么! :) - indyDean
5
希望我能给这个点赞多几次。感谢帮助!另外值得一提的是,我从这篇文章中注意到,“路径”部分不应该以“~”符号开头。 - Mercurybullet
1
如果您启用了自定义错误,则使用 @mhenry1384 的答案。 - RasikaSam
5
解决我的问题的关键是在httpErrors节点中添加属性existingResponse="Replace" errorMode="Custom"。 - Concept211
我来自这个答案,它让我抓狂了!!!你的解决方案完美地解决了我的问题。 - DanielV
显示剩余4条评论

6
<httpErrors existingResponse="PassThrough" />

在IIS 7(Windows Server 2008 R2)中对我有用。

我的问题是我把它放在了<location>中,但将其移动到根<system.webServer>中解决了问题。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    ...
    <system.webServer>
        ...
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>

1
你能贴出整个 web.config 文件吗?这样我们才能明白你在说什么。 - JGallardo
啊,抱歉。我很想帮忙,但我不再使用.NET,并且无法访问我的旧代码。 - mhenry1384
谢谢您将httpErrors元素放置在web.config上下文中,这样我们就知道该放在哪里了。 - yougotiger

0

对于IIS7及以上版本,请仅使用httpErrors,如rob的回答所示:https://dev59.com/0Gw15IYBdhLWcg3wYasx#6661699

只需补充说明,除非您需要/希望使用ASP.NET来呈现错误页面,否则我建议使用静态HTML文件以消除对ASP.NET的依赖性,只要确保省略任何前导斜杠并使用反斜杠作为路径的其余部分,例如:

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

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


这可能本来可以作为原始答案的评论。 - Abhinav Gauniyal
事后看来,你可能是对的。当时,我觉得我的观点足以需要一个新答案。 - James Rogers

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。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接