.net core MVC Filters 过滤器介绍
一、过滤器的优级依次介绍如下(逐次递减):
Authorization Filter -> Resource Filter -> Acton Filter -> Exception Filter -> Result Filter
的.net Core MVC 中同步过滤器,新增了异步过滤器,对数据可以进行异步处理。
1、Authorization Filter (权限过滤器)
/// <summary> /// 权限过滤器 /// </summary> public class GlobalAuthorizationFilter : Attribute, IAuthorizationFilter { /// <summary> /// 优化权最高的过滤器 /// </summary> /// <param name="context"></param> public void OnAuthorization(AuthorizationFilterContext context) { //context.Result = new ObjectResult("IAuthorizationFilter 过滤输出"); Console.WriteLine("IAuthorizationFilter 过滤输出"); } }
/// <summary> /// 异步权限过滤器 /// </summary> public class GlobalAsyncAuthorizationFilter : Attribute, IAsyncAuthorizationFilter { /// <summary> /// 执行异步Action 过滤器 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task OnAuthorizationAsync(AuthorizationFilterContext context) { Console.WriteLine("IAsyncAuthorizationFilter 拦截输出"); await Task.CompletedTask; } }
2、Resource Filter (资源过滤器)
/// <summary> /// 资源过滤器 /// </summary> public class GlobalResourceFilter : IResourceFilter { /// <summary> /// 获取资源后拦截 /// </summary> /// <param name="context"></param> public void OnResourceExecuted(ResourceExecutedContext context) { Console.WriteLine("IResourceFilter OnResourceExecuted 输出"); } /// <summary> /// 获取资源前拦截 /// </summary> /// <param name="context"></param> public void OnResourceExecuting(ResourceExecutingContext context) { Console.WriteLine("IResourceFilter OnResourceExecuting 输出"); } }
/// <summary> /// 异步资源加载过滤器 /// </summary> public class GlobalAsyncResourceFilter : IAsyncResourceFilter { /// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next) { // 执行前 await next.Invoke(); // 执行后 await OnExecutedAsync(context); } /// <summary> /// 执行后 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task OnExecutedAsync(ResourceExecutingContext context) { Console.WriteLine("IAsyncResourceFilter 执行后"); await Task.CompletedTask; } }
3、Acton Filter (Action 过滤器)
/// <summary> /// Acion过滤器 /// </summary> public class GlobalActonFilter : Attribute, IActionFilter { /// <summary> /// Acion 执行后拦截 /// </summary> /// <param name="context"></param> public void OnActionExecuted(ActionExecutedContext context) { Console.WriteLine("IActionFilter OnActionExecuted Acion 执行后拦截"); } /// <summary> /// Acion 执行前拦截 /// </summary> /// <param name="context"></param> public void OnActionExecuting(ActionExecutingContext context) { Console.WriteLine("IActionFilter OnActionExecuting Acion 执行前拦截"); } }
/// <summary> /// 异步 Action 过滤器 /// </summary> public class GlobalAsyncActonFilter : Attribute, IAsyncActionFilter { /// <summary> /// 执行异步Action 过滤器 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { // 执行前 await next.Invoke(); // 执行后 await OnExecutedAsync(context); } /// <summary> /// 执行后 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task OnExecutedAsync(ActionExecutingContext context) { Console.WriteLine("IAsyncActionFilter 执行后"); await Task.CompletedTask; } }
4、Exception Filter (异常过滤器)
/// <summary> /// 异常全局过滤器 /// </summary> public class GlobalExceptionFilter : IExceptionFilter { /// <summary> /// 异常处理 /// </summary> /// <param name="context">异常上下文</param> public void OnException(ExceptionContext context) { Console.WriteLine(context.Exception); //如果这里设为false,就表示告诉MVC框架,我没有处理这个错误。然后让它跳转到自己定义的错误页(设为true的话,就表示告诉MVC框架,异常我已经处理了。不需要在跳转到错误页了,也部会抛出黄页了) context.ExceptionHandled = false; } }
/// <summary> /// 异步异常全局过滤器 /// </summary> public class GlobalAsyncExceptionFilter : IAsyncExceptionFilter { /// <summary> /// 异常异步处理 /// </summary> /// <param name="context">异常上下文</param> /// <returns></returns> public async Task OnExceptionAsync(ExceptionContext context) { Console.WriteLine(context.Exception.Message); //如果这里设为false,就表示告诉MVC框架,我没有处理这个错误。然后让它跳转到自己定义的错误页(设为true的话,就表示告诉MVC框架,异常我已经处理了。不需要在跳转到错误页了,也部会抛出黄页了) context.ExceptionHandled = false; await Task.CompletedTask; }
5、Result Filter (结果过滤器)
/// <summary> /// 结果过滤器 /// </summary> public class GlobalResultFilter : IResultFilter { /// <summary> /// 动作结果执行后 /// </summary> /// <param name="context"></param> public void OnResultExecuted(ResultExecutedContext context) { Console.WriteLine("IResultFilter OnResultExecuted Acion 动作结果执行后"); } /// <summary> /// 动作结果执行前 /// </summary> /// <param name="context"></param> public void OnResultExecuting(ResultExecutingContext context) { Console.WriteLine("IResultFilter OnResultExecuting Acion 动作结果执行前"); } }
/// <summary> /// 异步 结果过滤器 /// </summary> public class GlobalAsyncResultFilter : IAsyncResultFilter { /// <summary> /// 动作结果执行后 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { await next.Invoke(); await OnExecutedAsync(context); } /// <summary> /// 执行后 /// </summary> /// <param name="context"></param> /// <returns></returns> public async Task OnExecutedAsync(ResultExecutingContext context) { Console.WriteLine("IAsyncResultFilter 执行"); await Task.CompletedTask; } }
Demo 源码:https://github.com/intotf/netCore/tree/master/WebFilters