【问题标题】:Web.config custom errors not working for 404'sWeb.config 自定义错误不适用于 404
【发布时间】:2011-08-01 03:29:08
【问题描述】:
<customErrors mode="On" defaultRedirect="~/Error.aspx">
    <error statusCode="500" redirect="~/500/"/>
    <error statusCode="404" redirect="~/404/"/>
</customErrors>

这适用于以下请求:

~/egerg.aspx
~/wf92734eg.aspx

但是对于任何其他扩展,这会提供服务器 404 页面!如何让服务器 404 页面重定向到我的错误页面?

【问题讨论】:

  • 你运行的是什么 IIS 版本?
  • @Daniel IIS 7 on Windows 7 Ultimate

标签: web-config http-status-code-404 custom-error-pages


【解决方案1】:

此解决方案适用于我的网站,该网站是 .NET Framework 4 上 99% 的网络表单和 1% 的 Razor/MVC 4。

  1. 在 /Web.config 中:

    <system.web>
      <customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx">
        <error statusCode="500" redirect="~/500/"/>
        <error statusCode="404" redirect="~/404/"/>
      </customErrors>
    </system.web>
    
  2. 在 404 处理程序的 Page_Load 事件中(我假设是 /404/default.aspx.cs):

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
    }
    
  3. 对 500 处理程序重复最后一步。

  4. /Global.asax.cs 中的 Application_Error 方法为空。

此解决方案与this one 非常相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2011-09-12
    • 2012-03-26
    相关资源
    最近更新 更多