无法使httpErrors正常工作

时间:2016-04-07 20:02:37

标签: c# asp.net-mvc error-handling web-config http-error

我已将以下内容添加到配置文件中。

<system.webServer>
  ...
  <httpErrors errorMode="Custom"
              existingResponse="Replace" 
              defaultResponseMode="ExecuteURL">
    <clear/>
    <error statusCode="404"
           responseMode="ExecuteURL"
           path="http://google.se" />
  </httpErrors>
</system.webServer>

然而,似乎我仍然得到黄色背景和堆栈跟踪的默认页面。我已经尝试在过滤器中注释错误处理并在system.web中添加/删除自定义错误。 (我正按this great article中的建议尝试httpErrors的方法。)

我错过了什么?我还能做些什么来解决问题?

1 个答案:

答案 0 :(得分:6)

您可以在ASP.NET级别执行此操作:

<system.web>
    ...
    <customErrors mode="On">
        <error statusCode="404" redirect="http://www.google.se"/>
    </customErrors>
</system.web>

如果你真的想在IIS级别点它,你可以这样:

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404"/>
        <error statusCode="404" path="http://www.google.fr" responseMode="Redirect"/>
    </httpErrors>
</system.webServer>

如果要重定向到绝对URL,则必须设置&#34; responseMode&#34;属性为&#34;重定向&#34;,&#34; ExecuteURL&#34;适用于动态投放内容,来自MSDN