全局异常处理特性
View
1 /// <summary> 2 /// 异常处理特性 3 /// </summary> 4 public class AutoExceptionAttribute : HandleErrorAttribute 5 { 6 public override void OnException(ExceptionContext filterContext) 7 { 8 //记录异常信息 9 Log.Instance.Write(filterContext.Exception); 10 base.OnException(filterContext); 11 } 12 }
使用方法
View
1 [AutoException] 2 public class AAController : Controller 3 { 4 // 5 // GET: /Admin/AA/ 6 7 public ActionResult Index() 8 { 9 var s = Authorized.Instance.GetTicket; 10 ViewBag.T = s.IssueDate; 11 ViewBag.TT = s.Expiration; 12 ViewBag.CT = Authorized.Instance.GetCookie.Expires; 13 throw new Exception("测试错误",new Exception("内部测试错误")); 14 return View(); 15 } 16 17 }
自定义异常页面配置,在Web.config中节点system.web内加上
<customErrors mode="On" redirectMode="ResponseRedirect"> <error redirect="Views/Shared/Error.cshtml" statusCode="404"/> </customErrors>


浙公网安备 33010602011771号