1.Action前和Action后
IActionFilter(还须要继承特性)
/// <summary> /// Action的Filter` /// </summary> public class CustomActionFilterAttribute : Attribute, IActionFilter { private ILogger<CustomActionFilterAttribute> _logger = null; public CustomActionFilterAttribute(ILogger<CustomActionFilterAttribute> logger) { this._logger = logger; } public void OnActionExecuted(ActionExecutedContext context) { //context.HttpContext.Response.WriteAsync("ActionFilter Executed!"); Console.WriteLine("ActionFilter Executed!"); //this._logger.LogDebug("ActionFilter Executed!"); } public void OnActionExecuting(ActionExecutingContext context) { //context.HttpContext.Response.WriteAsync("ActionFilter Executing!"); Console.WriteLine("ActionFilter Executing!"); //this._logger.LogDebug("ActionFilter Executing!"); } } /// <summary> /// 标记到Controller /// </summary> public class CustomControllerActionFilterAttribute : Attribute, IActionFilter { private ILogger<CustomControllerActionFilterAttribute> _logger = null; public CustomControllerActionFilterAttribute(ILogger<CustomControllerActionFilterAttribute> logger) { this._logger = logger; } public void OnActionExecuted(ActionExecutedContext context) { //context.HttpContext.Response.WriteAsync("ActionFilter Executed!"); Console.WriteLine("ActionFilter Executed!"); //this._logger.LogDebug("ActionFilter Executed!"); } public void OnActionExecuting(ActionExecutingContext context) { //context.HttpContext.Response.WriteAsync("ActionFilter Executing!"); Console.WriteLine("ActionFilter Executing!"); //this._logger.LogDebug("ActionFilter Executing!"); } }
2. 异常处理的Filter
ExceptionFilterAttribute
/// <summary> /// 异常处理的Filter /// </summary> public class CustomExceptionFilterAttribute : ExceptionFilterAttribute { private readonly IHostingEnvironment _hostingEnvironment; private readonly IModelMetadataProvider _modelMetadataProvider; //private Logger logger = Logger.CreateLogger(typeof(CustomExceptionFilterAttribute)); /// <summary> /// ioc来的 /// </summary> /// <param name="hostingEnvironment"></param> /// <param name="modelMetadataProvider"></param> public CustomExceptionFilterAttribute( IHostingEnvironment hostingEnvironment, IModelMetadataProvider modelMetadataProvider) { _hostingEnvironment = hostingEnvironment; _modelMetadataProvider = modelMetadataProvider; } /// <summary> /// 没有处理的异常,就会进来 /// </summary> /// <param name="filterContext"></param> public override void OnException(ExceptionContext filterContext) { if (!filterContext.ExceptionHandled)//异常有没有被处理过 { string controllerName = (string)filterContext.RouteData.Values["controller"]; string actionName = (string)filterContext.RouteData.Values["action"]; string msgTemplate = "在执行 controller[{0}] 的 action[{1}] 时产生异常"; //logger.Error(string.Format(msgTemplate, controllerName, actionName), filterContext.Exception); if (this.IsAjaxRequest(filterContext.HttpContext.Request))//检查请求头 { filterContext.Result = new JsonResult( new { Result = false, PromptMsg = "系统出现异常,请联系管理员", DebugMessage = filterContext.Exception.Message }//这个就是返回的结果 ); } else { var result = new ViewResult { ViewName = "~/Views/Shared/Error.cshtml" }; result.ViewData = new ViewDataDictionary(_modelMetadataProvider, filterContext.ModelState); result.ViewData.Add("Exception", filterContext.Exception); filterContext.Result = result; } filterContext.ExceptionHandled = true; } } private bool IsAjaxRequest(HttpRequest request) { string header = request.Headers["X-Requested-With"]; return "XMLHttpRequest".Equals(header); } }
3.请求页面前,没有进控制器
IResourceFilter
/// <summary> /// 自定义的资源Filter /// </summary> public class CustomResourceFilterAttribute : Attribute, IResourceFilter { private static readonly Dictionary<string, object> _Cache = new Dictionary<string, object>(); private string _cacheKey; /// <summary> /// 控制器实例化之前 /// </summary> /// <param name="context"></param> public void OnResourceExecuting(ResourceExecutingContext context) { _cacheKey = context.HttpContext.Request.Path.ToString(); if (_Cache.ContainsKey(_cacheKey)) { var cachedValue = _Cache[_cacheKey] as ViewResult; if (cachedValue != null) { context.Result = cachedValue;// 短路器 } } } /// <summary> /// 把请求都处理完的 /// </summary> /// <param name="context"></param> public void OnResourceExecuted(ResourceExecutedContext context) { if (!String.IsNullOrEmpty(_cacheKey) && !_Cache.ContainsKey(_cacheKey)) { var result = context.Result as ViewResult; if (result != null) { _Cache.Add(_cacheKey, result); } } } }
4.返回结果
IResultFilter
/// <summary> /// Result的Filter /// </summary> public class CustomResultFilterAttribute : Attribute, IResultFilter { //private Logger logger = Logger.CreateLogger(typeof(CustomResultFilterAttribute)); public void OnResultExecuted(ResultExecutedContext context) { Console.WriteLine("OnResultExecuted Executed!"); //logger.Info("OnResultExecuted Executed!"); } public void OnResultExecuting(ResultExecutingContext context) { Console.WriteLine("OnResultExecuting Executing!"); //logger.Info("OnResultExecuting Executing!"); } }