.net IIS7 自定义 404 未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6648007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:42:49  来源:igfitidea点击:

IIS7 custom 404 not showing

.netiis-7http-status-code-404

提问by rob

created a new IIS7 web site with Intergrated .net 4.0 app pool.

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

URLs ending with .aspx do show custom 404 anything else gives the blue server error page "HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." (so nothing to do with IE)

以 .aspx 结尾的 URL 会显示自定义 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>

also tried

也试过

<httpErrors existingResponse="PassThrough" />

but that just resulted in an empty response.

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

I have only found one reference to the usefulness of executing the appcmd to test the custom http error handling but here are the results.

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

which is odd because in iis7 manager the error pages show

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

404 /pages/404.aspx Execute URL Local

.Net Error Pages shows nothing, though I did have an entry in there.

.Net 错误页面什么也没显示,尽管我确实在那里有一个条目。

Question 1 : what steps do I need to take for a completely new asp .net 4 iis7 site to have a custom .net error page for every 404 result ?

问题 1:对于一个全新的 asp .net 4 iis7 站点,我需要采取哪些步骤才能为每个 404 结果创建一个自定义的 .net 错误页面?

Question 2 : why does the .net handler work for .aspx files and nothing else ?

问题 2:为什么 .net 处理程序只对 .aspx 文件起作用,而没有其他作用?

note: set the 404 at server level and the appcmd command then showed the custom 404 in the path, but made no difference to the site failing to showing 404.

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

So I am guessing it's a red herring.

所以我猜这是一条红鲱鱼。

回答by rob

answer was to use

答案是使用

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

and not to have any system.web customErrors

并且没有任何 system.web customErrors

this worked for both .aspx and non .aspx requests.

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

bizarrely this combination did not come up in any of the blog posts and stackoverflow answers I had investigated, it was just luck I tried it.

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

回答by mhenry1384

<httpErrors existingResponse="PassThrough" />

worked for me in IIS 7 (Windows Server 2008 R2).

在 IIS 7(Windows Server 2008 R2)中对我来说有效。

My problem was I had this in my web.config, but in a <location>. Moving it to the root <system.webServer>fixed it.

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

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

回答by James Rogers

For IIS7 onwards, go with httpErrors only, as per rob's answer: https://stackoverflow.com/a/6661699

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

Just to add, unless you need/want to use ASP.NET to render your error page, I would recommend using static HTML files to remove the dependency on ASP.NET. Just be sure to omit any leading forward slash and use backslashes for the rest of the path, e.g.

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

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

Set responseMode="File"to retain the correct status code.

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

回答by Dennis R

I was having this issue with my web app and I got it resolved by commenting out the redirect line:

我的网络应用程序遇到了这个问题,我通过注释掉重定向行解决了这个问题:

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

On IIS, the allowAbsolutePathsWhenDelegated (on Configuration Editor --> system.webServer/httpErrors) was locked and I can't change the value false to true.

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