HandleErrorAttribute继承自FilterAttribute,且实现了IExceptionFilter接口。
属于AOP思想的一种实现,MVC的四大筛选器(权限,动作,结果,异常)中的异常处理。
public class AppHandleErrorAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { var json = new JsonResponse(); json.success = false; var ex = filterContext.Exception; //deal custom exception… json.message = "系统异常,请联系管理员!"; string msg = string.Format("系统异常:{0}", ex.Message); LogHelper.Error(msg, ex); //Log4jNet filterContext.ExceptionHandled = true; var result = new JsonResult(); result.Data = json; result.JsonRequestBehavior = JsonRequestBehavior.AllowGet; filterContext.Result = result; } }
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new AppHandleErrorAttribute()); } }
参考:http://www.cnblogs.com/linjiancun/archive/2010/09/16/1827937.html
MVC 使用HandleErrorAttribute统一处理异常,布布扣,bubuko.com
MVC 使用HandleErrorAttribute统一处理异常
原文:http://www.cnblogs.com/pengzhen/p/3774814.html