【问题标题】:Can I tell IIS 8.5 to return a 404 when using httpErrors?我可以告诉 IIS 8.5 在使用 httpErrors 时返回 404 吗?
【发布时间】:2015-12-28 20:52:48
【问题描述】:

我正在开发一个在 IIS 8.5 上运行的(Web 窗体)站点,目前正在为 404 和 500 设置错误处理。

我已经有了system.web > customErrors 设置,但是由于我们在集成模式/更新版本的 IIS 下运行,它从system.webServer > httpErrors 中提取。

使用 IIS GUI,我选择在网站上用静态 HTML 文件替换标准 IIS 错误页面:

<httpErrors errorMode="Custom">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" prefixLanguageFilePath=""
        path="/problem.html" responseMode="ExecuteURL" />
    <remove statusCode="500" subStatusCode="-1" />
    <error statusCode="500" prefixLanguageFilePath=""
        path="/problem.html" responseMode="ExecuteURL" />
</httpErrors>

不幸的是,当显示正确的错误页面时,不是返回 404,而是返回 200,对于 500 错误,返回 302 后跟 200。

对于旧版本的 IIS,或未在集成模式下运行的 IIS,您可以在 customErrors 元素上设置 redirectMode="ResponseRewrite",这将解决此问题。

我尝试将httpErrors 上的existingResponse 设置为所有三个可用选项,并尝试更新设置responseMode="File",但这些都没有改变。

有没有办法为httpErrrors element 做类似的事情?还是我坚持将错误指向 ASP.NET 页面并从那里返回适当的错误?

谢谢!

【问题讨论】:

    标签: asp.net iis-8.5 custom-errors integrated-pipeline-mode


    【解决方案1】:

    要解决此问题,您可以将此配置设置为 WebConfig 文件中的 &lt;httpErrors&gt;。像这样:

    <system.webServer>
        <httpErrors errorMode="Custom" existingResponse="Replace">
          <clear/>
          <error statusCode="404" path="/WebForms/Index.aspx" responseMode="File"/>
        </httpErrors>
    <system.webServer/>
    

    responseMode=File 将保留原始错误代码,并显示静态页面。

    【讨论】:

    • 看起来existingResponse="Replace"responseMode="File" 的组合是诀窍。但是,对于静态 HTML,我还必须删除初始斜线 path="problem.html"(否则它会返回白页错误)。我还必须清除 customErrors 元素(好吧,模式刚刚设置为 RemoteOnly
    • 您也可以在httpErrors 元素上使用defaultResponseMode="File" 以避免在多个error 节点上声明它
    猜你喜欢
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2016-10-11
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    相关资源
    最近更新 更多