著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。作者:卷猫链接:http://anneke.cn/ArticleInfo/Detial/19来源:Anneke.cnpublic class OwnActionFilter : ActionFilterAttribute,IAuthorizationFilter,IExceptionFilter { ////// 最先执行 /// /// public void OnAuthorization(AuthorizationContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnAuthorization"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } ////// 在action method之前执行 /// /// public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnActionExecuting"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } ////// 在action method之后执行 /// /// public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnActionExecuted"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } ////// 在返回view之前执行 /// /// public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnResultExecuting"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } ////// 在返回View之后执行 /// /// public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnResultExecuted"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } ////// 当出现异常时才执行 /// /// public void OnException(ExceptionContext filterContext) { filterContext.HttpContext.Response.Write("方法名称:OnException"); //输出请求的控制器与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("控制器:{0}action:{1}", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------华丽的分割线------------------"); } }
执行效果: