c# - 自定义错误页面的重定向循环

标签 c# asp.net .net redirect

为了错误处理,我在 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 重定向会正常工作。仍在寻找一种重定向到项目页面而不会无限循环错误的方法

最佳答案

尝试添加:

Server.ClearError();

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

关于c# - 自定义错误页面的重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22690639/

相关文章:

c# - "Substring"一个数值

c# - Windows 窗体 : "System.ArgumentException: Parameter is not valid." coming from System stack

c# - 如何在 C# 中获取利率和银行同业拆借利率?

c# - 根据属性名称数组创建对象的子集

c# - 如何比较不区分大小写和不区分重音的字符串

.net - 使用 LinqToSQL 更新多对多关系

c# - ContentType.Xml 不存在?

c# - Twitter Bootstrap 和 ASP.NET GridView

c# - 如何在.NET Framework 4.6.1中使用IActionResult?

c# - "foreach with task.Wait"和 Task.WaitAll 的区别