自定义错误在404的实际网站中无效

时间:2014-02-13 08:36:50

标签: asp.net iis iis-7.5

我正在尝试为我的网站设置自定义错误,该错误适用于本地主机mode="On"

但在实际网站上也失败了。

以下是我的web.config代码

<customErrors mode="RemoteOnly" defaultRedirect="~/error/Message.aspx?msg=The page you requested could not be found.">
  <error statusCode="404" redirect="~/error/Default.aspx?PID=32"/>
  <error statusCode="403" redirect="~/error/Forbidden.aspx"/>
  <error statusCode="400" redirect="~/error/Error.aspx"/>
  <error statusCode="500" redirect="~/error/InternalError.aspx"/>
</customErrors>

我在本地计算机上使用IIS 7.5,并且正在Web服务器上使用它。

我是否需要更改生产服务器上的任何特定设置才能使其正常工作

错误消息我仍然访问404的实时网站

Not Found
The requested document was not found on this server.
Web Server at xyz.com

2 个答案:

答案 0 :(得分:2)

以下代码的最终版本

...
<customErrors mode="RemoteOnly" defaultRedirect="~/error/Message.aspx?msg=The page you requested could not be found.">
  <error statusCode="404" redirect="~/error/Default.aspx?PID=32"/>
  <error statusCode="403" redirect="~/error/Forbidden.aspx"/>
  <error statusCode="400" redirect="~/error/Error.aspx"/>
  <error statusCode="500" redirect="~/error/InternalError.aspx"/>
</customErrors>

....

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" path="/error/Default.aspx?PID=32" responseMode="ExecuteURL"/>
    </httpErrors>
  </system.webServer>

答案 1 :(得分:1)

web.config部分似乎没问题,即使我建议使用静态html进行默认的错误页面重定向。

如何在远程服务器上测试此问题? 输入网址 xyz.com/xxxxxx ,其中xxxxxx是不存在的页面?

如果是,那么xxxxxx没有文件扩展名吗? 如果xxxxx以文件扩展名结尾(这是网站默认页面之一),404重定向是否有效?的 e.g。 xxxx.aspx

试试这篇文章 asp.net custom error page and file without extension