ASP.NET MVC 异常未正确路由到 Customerrors 默认路由
我遇到了将应用程序显示为页面中堆栈跟踪的异常问题。
我认为我已经通过将其添加到 web.config
来缓解这个问题:
<customErrors mode="On" defaultRedirect="~/error/GenericError">
<error statusCode="403" redirect="~/error/NoAccess" />
<error statusCode="404" redirect="~/error/NotFound" />
</customErrors>
它适用于不存在的路由,但不适用于控制器抛出异常。这是控制器逻辑:
[HandleError]
public class DebugController : Controller
{
public ActionResult Index()
{
throw new Exception("** Testing custom error Handling **");
return View();
}
}
public class ErrorController : Controller
{
//
// GET: /Error/
public ActionResult NotFound()
{
ViewData["error"] = "That page does not exist.";
return View();
}
public ActionResult GenericError()
{
if (null == TempData["error"]))
{
ViewData["error"] = "We're sorry, but an error has occurred.";
}
else
{
ViewData["error"] = TempData["error"];
}
return View();
}
public ActionResult NoAccess()
{
ViewData["error"] = "You are not authorized to access application";
return View();
}
}
这是视图:
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/CenterContentNoSidebar.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
GenericError
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>An Error has occurred</h2>
<br />
<%= ViewData["error"] %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="NavContent" runat="server">
</asp:Content>
我是否必须在 Global.asax 中添加一些内容才能使其工作?
I'm having an issue with exceptions showing app as stack traces in the page.
I thought that I had mitigated this by adding this to the web.config
:
<customErrors mode="On" defaultRedirect="~/error/GenericError">
<error statusCode="403" redirect="~/error/NoAccess" />
<error statusCode="404" redirect="~/error/NotFound" />
</customErrors>
It works for non-existent routes, but not when a controller throws an exception. Here's the Controller logic:
[HandleError]
public class DebugController : Controller
{
public ActionResult Index()
{
throw new Exception("** Testing custom error Handling **");
return View();
}
}
public class ErrorController : Controller
{
//
// GET: /Error/
public ActionResult NotFound()
{
ViewData["error"] = "That page does not exist.";
return View();
}
public ActionResult GenericError()
{
if (null == TempData["error"]))
{
ViewData["error"] = "We're sorry, but an error has occurred.";
}
else
{
ViewData["error"] = TempData["error"];
}
return View();
}
public ActionResult NoAccess()
{
ViewData["error"] = "You are not authorized to access application";
return View();
}
}
And here's the View:
<%@ Page Title="" Language="C#"
MasterPageFile="~/Views/Shared/CenterContentNoSidebar.Master"
Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
GenericError
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>An Error has occurred</h2>
<br />
<%= ViewData["error"] %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="NavContent" runat="server">
</asp:Content>
Do I have to put something in the Global.asax to make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,HandleError 属性实际上正在处理错误并尝试从控制器视图文件夹或共享位置返回 Error.aspx 视图。 customErrors web.config 部分永远不会被点击,并且由于该页面可能不存在,所以无论如何都会显示黄屏死机。不存在的路由正在工作,因为 IIS 抛出 404 错误并且没有控制器或 HandleError 属性的上下文。如果您想自己处理错误,我建议删除 HandleError 属性并让 customErrors 一些通过。不过,从错误控制器中,您必须捕获最后一个异常并对其执行某些操作。这是我正在谈论的内容的一个很好的参考。
http://blog.dantup.com/2009/ 04/aspnet-mvc-handleerror-attribute-custom.html
它是一种或另一种类型的东西,HandleError 属性或 customErrors web.config 部分。
So The HandleError Attribute is actually handling the error and trying to return an Error.aspx view from the controllers view folder or the shared location. The customErrors web.config section is never being hit, and since the page might not exist the yellow screen of death is being displayed anyways. The non-existent routes is working cause IIS is throwing a 404 error and has no context of a controller or HandleError attribute. If you want to handle the errors yourself I would suggest removing the HandleError attributes and let the customErrors some through. From your error controller though you would have to grab the last exception though and do something with it. Here is a good reference on what I am talking about a little.
http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html
It is kind of a one or the other type of thing, HandleError attribute or customErrors web.config section.
您看到此行为是因为您将
[HandleError]
放置在顶行。请参阅 Stack 上的此问题/答案,了解有关[HandleError]
属性的更多信息。StackOverflow 处理错误问答
Your seeing this behavior because of the
[HandleError]
that you placed on your top line. Please see this qusetion/answer on Stack for more information on the[HandleError]
attribute.StackOverflow Handle Error Q&A