我遇到了将应用程序显示为页面中的堆栈跟踪的异常问题。
我认为我已经通过将其添加到以下内容来缓解这种情况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 中添加一些内容才能使其正常工作?