【问题标题】:Redirect Loop for Custom Errors Page自定义错误页面的重定向循环
【发布时间】:2014-05-06 14:31:07
【问题描述】:

对于错误处理,我在 Web.config 中添加了以下代码:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error.cshtml?">
    </customErrors>
<system.web>

在 Global.asax.cs 中:

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    Response.Redirect("/Error.cshtml?");
}

但是,当重定向到/Error.cshtml 时呢?发生时,出现错误“此页面有重定向循环”

我搜索过其他类似的问题(例如How to solve Redirect Loop)。但是,我没有其他调用 Error.cshtml 的路由——那么有谁知道是什么导致了这个循环以及如何解决它?

编辑:发生了多个错误,因此错误页面重定向涉及重定向循环。有谁知道只调用一次 Response.Redirect("~/Error.cshtml") 的方法?

EDIT2:如果我将重定向 url 更改为项目外部的随机 url(例如 https://www.facebook.com/),customError 重定向将正常工作。仍在寻找一种方法来重定向到项目页面而不会无限循环错误

【问题讨论】:

  • Error.cshtml 中有什么?
  • 我只有一些

    test text

    类型的标记,没有什么应该引起任何类型的循环

  • @suhMAN,你确定拨打Response.Redirect后没有出错吗?
  • 我刚刚测试过,看起来我确实会继续出现在 Application_Error 函数中。我发现 Error.cshtml 实际上使用了@model System.Web.Mvc.HandleErrorInfo——这会有什么影响吗?
  • 可能是 404 尝试检索错误页面。试试 ~/Error.cshtml

标签: c# asp.net .net redirect


【解决方案1】:

将 redirectMode="ResponseRewrite" 添加到 customErrors 部分。

【讨论】:

  • 当我将它添加到 customErrors 时也会出现同样的错误
  • @suhMAN 同时删除 Response.Redirect("/Error.cshtml?");
  • 当我在Application_Error函数中删除Response.Redirect时,没有发生重定向,并且显示一个空白页面
  • 你确定 /Error.cshtml 是一个有效的请求吗?通常 .cshtml 文件是视图。你可以在行动中使用它,所以像 request /Common/Error 渲染 Error.cshtml 视图。
【解决方案2】:

尝试添加:

Server.ClearError();

在重定向之前首先清除现有的错误。

【讨论】:

  • 我尝试添加那行代码,但同样的错误仍然存​​在
猜你喜欢
  • 2020-12-23
  • 2021-10-30
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-23
  • 2014-07-23
  • 2022-10-12
  • 1970-01-01
相关资源
最近更新 更多