【问题标题】:MVC5 Exception on ActionFilterAttribute captured by a custom HandleErrorAttribute changes the StackTrace自定义 HandleErrorAttribute 捕获的 ActionFilterAttribute 上的 MVC5 异常更改了 StackTrace
【发布时间】:2016-06-16 13:21:53
【问题描述】:

我有一个这样的 ActionFilterAttribute

public class DataModelAttribute : ActionFilterAttribute
{   
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        ...
        throw filterContext.Exception;  //StackTrace is fine (pointing to my controller)
    }
}

我有一个像这样的全局错误过滤器:

public class OncHandleErrorAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext context)
    {
        var ex = context.Exception; //Here StackTrace points to DataModelAttribute file
    }
}

所以我丢失了我原来的 StackTrace ...我该如何保存它?

【问题讨论】:

标签: asp.net-mvc


【解决方案1】:

我猜你不能像MSDN 所建议的那样只使用throw 而不是throw filterContext.Exception

所以你应该抛出一个新异常并将原始异常包含为内部异常,如建议的here

【讨论】:

    【解决方案2】:

    在你的情况下,它看起来像这样:

     public class HandleExceptionErrors : ActionFilterAttribute, IExceptionFilter
    {
    
        private Type _exceptionType;
        public HandleExceptionErrors(Type exceptionType)
        {
            _exceptionType = exceptionType;
        }
        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.Exception.GetType() != _exceptionType) return;
    
    
            filterContext.ExceptionHandled = true;
            var data= filterContext.Exception +" , "+ filterContext.Exception.InnerException+" , "+filterContext.Exception.Message +" , "+filterContext.Exception.StackTrace;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2015-10-06
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 1970-01-01
      相关资源
      最近更新 更多