【问题标题】:How to change the default behaviour for customErrors pages in Asp.Net如何更改 Asp.Net 中 customErrors 页面的默认行为
【发布时间】:2011-11-08 06:46:38
【问题描述】:

我使用 Asp.Net 4 和 C#。

我想知道如何更改 Asp.Net customErrors 的默认行为。

在我的Web.Config 文件中,我使用此设置。

    <customErrors mode="On" defaultRedirect="/ErrorPages/Oops.aspx">
        <error statusCode="404" redirect="/ErrorPages/404.aspx" />
    </customErrors>

正如您所料,如果找不到页面,则会重定向到 Web.Config 中指定的自定义页面。


请求的页面:

http://localhost:1372/nopagehere

结果页面:

http://localhost:1372/ErrorPages/404.aspx?aspxerrorpath=/nopagehere

当我分析 Result 页面的 Http Header 时,我可以看到:

  • 找不到页面时的 404 状态
  • 新创建的 url 的 302 状态

我想以这种方式改变这种行为:

知道怎么做吗?感谢您抽出宝贵时间。

【问题讨论】:

    标签: c# asp.net html http-status-code-404


    【解决方案1】:

    我猜你是在 &lt;customErrors&gt; 部分寻找 redirectMode 属性。

    见:http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx

    我认为您想将其设置为 redirectMode="ResponseRewrite"。这会引发 404 并显示 404 页面的内容,但不会重定向到实际的 404 页面。

    【讨论】:

    • 我想删除重定向,只为找不到页面发送 404 状态
    • 谢谢 Jan 按预期工作我不知道这个属性。
    • 我这里有个很奇怪的问题stackoverflow.com/questions/7269103/…有时间看看,不胜感激谢谢
    • 该属性是 v4.0 的新属性
    • 我很确定 redirectMode 自 2.0 以来就已经存在
    【解决方案2】:

    也许从自定义错误中删除状态代码 404 并让默认重定向页面处理它。

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2019-10-13
      • 2019-05-30
      • 2017-07-11
      • 2023-03-09
      • 2018-11-14
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      相关资源
      最近更新 更多