【问题标题】:How to re-route exception without marking Exception as handled如何重新路由异常而不将异常标记为已处理
【发布时间】:2015-07-15 02:47:30
【问题描述】:

基本上,当我的应用程序有未处理的异常时,我希望它被 IIS 记录为未处理的异常(例如,以便可以在事件查看器上看到),但我也想将用户路由到错误控制器。

我已经覆盖了我的控制器的OnException 方法,以便将用户路由到自定义错误页面。问题的症结在于这段代码:

    protected override void OnException(ExceptionContext filterContext)
    {
        filterContext.Result = RedirectToAction("GeneralError", "Error", new{ routeValueA = "some value", routeValueB = "some other value"});
        filterContext.ExceptionHandled = false;
    }

我的问题是这样的:如果我设置了filterContext.ExceptionHandled = false,那么我会得到一个黄屏死机,而不是被重新路由到我的错误处理控制器。如果我设置filterContext.ExceptionHandled = true,那么我会被重新路由,但异常不会记录为未处理的异常。

我知道我可以使用 web.config 设置静态错误页面,但我不想这样做,因为这样我就不能使用路由值动态地将数据发送到我的错误控制器。

我能否成功地将结果设置到我的filterContext.Result 而不标记filterContext.ExceptionHandled= true

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-4 iis exception-handling


    【解决方案1】:

    尝试从 this 来源

    protected override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.ExceptionHandled)
        {
            return;
        }
        filterContext.Result = new ViewResult
        {
            ViewName = "~/Views/Shared/Error.aspx"
        };
        filterContext.ExceptionHandled = true;
    }
    

    或者你也可以试试这个

    web.config中设置custom errors如下:

    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/NotFound" statusCode="404" />
      <error redirect="~/Error/UnauthorizedAccess" statusCode="403"/>
    </customErrors>
    

    你的ErrorController

    public class ErrorController : Controller
    {
        public ViewResult Index()
        {
            return View("Error");
        }
        public ViewResult NotFound()
        {
            Response.StatusCode = 404;  //you may want to set this to 200
            return View("NotFound");
        }
        public ViewResult UnauthorizedAccess()
        {
            Response.StatusCode = 404;  //you may want to set this to 200
            return View("UnauthorizedAccess");
        }
    }
    

    HandleErrorAttribute注册为FilterConfig类中的全局操作过滤器,如下所示:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
         filters.Add(new CustomHandleErrorAttribute());
         filters.Add(new AuthorizeAttribute());
    }
    

    更新

    我建议您阅读this answer,因为它提供了您所提出问题的完整细节,我希望您能在那里找到一个好的解决方案!

    【讨论】:

    • 感谢您的回答,但这并不能解决我的问题:我想动态重定向(即不使用 web.config),但也将异常标记为未处理 - 这样IIS 将其视为未处理的异常。我已经编辑了问题以使其更清晰
    • @ChrisRedhead.. 检查 更新 部分.. :)
    • 谢谢。在我看来,不可能做我想做的事——即基本上处理异常,但让 IIS 认为它未处理——但我找不到明确的来源来确认。我会把这个打开一两天,看看是否有人有,然后我会接受这个作为最有帮助的答案。
    • 太棒了@ChrisRedhead .. 同时,如果可用,我将尝试获取任何可能的解决方案来解决您的问题.. 快乐编码.. :)
    【解决方案2】:

    我相信您正在寻找的是您可以在 web.config 文件中设置的自定义错误页面,并告诉 IIS 重定向到自定义错误页面而不是默认页面(因为您称之为死亡黄页 :))对于任何未处理的异常。

    这可能会有所帮助: How to make custom error pages work in ASP.NET MVC 4

    【讨论】:

    • 感谢您的回答,但这并不能解决我的问题。我希望能够动态处理异常,但仍然将异常标记为未处理。我已更新问题以使其更清晰
    猜你喜欢
    • 2015-01-26
    • 2023-03-25
    • 2015-11-03
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多