/// <summary> /// MVC自定义过滤器 /// </summary> public class LoggingFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Trace.Write("Starting:"+ filterContext.ActionDescriptor.ActionName); } //错误过滤器 public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Exception != null) { filterContext.HttpContext.Trace.Write("Exception thrown"); } } }
引用:
[LoggingFilterAttribute]
public ActionResult Index(string type)
{
//故意引发错误,则自动会执行 LoggingFilterAttribute过滤器中的OnActionExecuted方法</span> int i = 7, j = 0; int c = i/j; return View(); }