標籤:c style class blog code java
HandleErrorAttribute繼承自FilterAttribute,且實現了IExceptionFilter介面。
屬於AOP思想的一種實現,MVC的四大篩選器(許可權,動作,結果,異常)中的異常處理。
Usage1.建立自訂異常處理
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; }}
2.註冊
public class FilterConfig{ public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new AppHandleErrorAttribute()); }}
參考:http://www.cnblogs.com/linjiancun/archive/2010/09/16/1827937.html