【问题标题】:IIS7 custom 404 not showingIIS7 自定义 404 未显示
【发布时间】:2011-10-02 15:27:24
【问题描述】:

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

以 .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>

也试过了

<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:对于一个全新的 asp .net 4 iis7 站点,我需要采取哪些步骤才能为每个 404 结果生成一个自定义 .net 错误页面?

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

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

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

【问题讨论】:

    标签: .net iis-7 http-status-code-404


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      对于 IIS7 及更高版本,仅使用 httpErrors,根据 rob 的回答:https://stackoverflow.com/a/6661699

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

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

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

      【讨论】:

      • 这可能是对原始答案的评论。
      • 事后看来,您可能是对的。当时我觉得我的观点很重要,足以保证一个新的答案。
      【解决方案3】:
      <httpErrors existingResponse="PassThrough" />
      

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

      我的问题是我的 web.config 中有这个,但在 &lt;location&gt; 中。将其移至根目录 &lt;system.webServer&gt; 已修复。

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

      【讨论】:

      • 你能把整个web.config 发上来让我们看看你在说什么吗?
      • 啊,对不起。我想帮忙,但我不再使用 .NET,也无法访问我的旧代码。
      • 感谢您将 httpErrors 元素放在 web.config 的上下文中,这样我们就知道该放在哪里了。
      【解决方案4】:

      答案是使用

          <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 答案中,我尝试过只是运气。

      【讨论】:

      • 感谢您发布此答案 - 这让我看起来好像知道自己在做什么! :)
      • 希望我能多投票几次。感谢您的帮助!...另外值得一提的是,我从这篇文章中注意到,“路径”部分不应以“~”开头
      • 谢谢 - 当通过 IIS 进行设置时,不会设置 httpError 元素的属性。这是微软的失败!他们为什么要把它放进去而不是记录下来?
      • 如果您有自定义错误,请使用 @mhenry1384 的答案
      • 解决我的问题的关键是将existingResponse="Replace" errorMode="Custom" 属性添加到httpErrors 节点。
      猜你喜欢
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      相关资源
      最近更新 更多