首页 技术 正文
技术 2022年11月21日
0 收藏 661 点赞 3,002 浏览 3574 个字

过滤器的作用是在 Action 方法执行前或执行后做一些加工处理。使用过滤器可以避免Action方法的重复代码,例如,您可以使用异常过滤器合并异常处理的代码。

过滤器如何工作?

过滤器在 MVC Action 调用管道中运行,有时称为过滤器管道。MVC选择要执行的Action方法后,才会执行过滤器管道:

实现

过滤器同时支持同步和异步两种不同的接口定义。您可以根据执行的任务类型,选择同步或异步实现。

同步过滤器定义OnStageExecuting和OnStageExecuted方法,会在管道特定阶段之前和之后运行代码的。例如IActionFilter过滤器,在调用Action方法之前调用OnActionExecuting,在Action方法之回之后调用OnActionExecuted

    public class SampleActionFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
// do something before the action executes
} public void OnActionExecuted(ActionExecutedContext context)
{
// do something after the action executes
}
}

异步过滤器定义了一个OnStageExecutionAsync方法。该方法提供了FilterTypeExecutionDelegate的委托,当调用该委托时会执行具体管道阶段的工作。例如,ActionExecutionDelegate用于调用Action方法,您可以在调用它之前和之后执行代码。

    public class SampleAsyncActionFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(
ActionExecutingContext context,
ActionExecutionDelegate next)
{
// do something before the action executes
await next();
// do something after the action executes
}
}

您可以在单个类中实现多个过滤器接口。例如,ActionFilterAttribute抽象类实现了IActionFilterIResultFilter,以及与它们对应的异步接口。

提示

您不需要同时实现两种过滤器接口,要么是同步的,要么是异步的。框架首先检查过滤器是否实现了异步接口,如果是,直接执行异步方法。如果不是,它会执行同步接口的方法。如果在一个类上同时实现两种接口,则只会调用异步方法。当使用像ActionFilterAttribute这类抽象类时,您只需要覆盖过滤器的同步方法或异步方法。

## 过滤器类型

ASP.NET Core 有以下五种类型的过滤器,每个过滤器类型在过滤器管道中的不同阶段执行:

  1. Authorization Filter

    授权过滤器 在过滤器管道中第一个执行,通常用于验证当前请求的合法性,不合法后面的管道会直接跳过。它们只有一个Before方法,不像其它大多数过滤器支持前置阶段方法和后置阶段方法。注意,您不要在授权过滤器中抛出异常,因为没有任何代码来处理异常(异常过滤器不处理它们)。

  2. Resource Filter

    资源过滤器是第二个运行,在 Authorization Filter 之后,Model Binding 之前执行。在性能方面,资源过滤器在实现缓存或截断过滤器管道尤为重要。

  3. Action Filter

    使用率最高的过滤器,在调用 Acioin 方法之前和之后执行代码。跟 Resource Filter 很类似,但 Model Binding 在之后执行。

  4. Exception Filter

    用于为应用程序执行异常处理策略。

  5. Result Filter

    当 Action 执行完成后,最后会执行过滤器。用于处理ActionResult结果输出策略。

## 过滤器运行顺序
ASP.NET Core 的每个请求都会先经过已注册的`Middleware`,接着才会执行过滤器:同类型的过滤器都会以先进后出的方式执行。

图片来自 John Wu 的博客

黃色箭头是正常情況流程

灰色箭头是异常处理流程

过滤器的作用域与执行顺序

过滤器具有三种不同级别的作用域。您可以通过Attribute将过滤器注册到指定控制器或 Action 方法;您也可以在Startup类的ConfigureServices方法中将过滤器注册到MvcOptions.Filters的集合中作为全局过滤器(对所有的控制器和Action方法均有效):

    public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Filters.Add(new AddHeaderAttribute("GlobalAddHeader",
"Result filter added to MvcOptions.Filters")); // an instance
options.Filters.Add(typeof(SampleActionFilter)); // by type
options.Filters.Add(new SampleGlobalActionFilter()); // an instance
}); services.AddScoped<AddHeaderFilterWithDi>();
}
}

示例来自于ASP.NET Core MVC 英语文档

默认执行顺序

当管道的某个阶段存在多个过滤器时,过滤器执行的默认顺序由作用域确定:全局过滤器优先于控制器过滤器,控制器过滤器优先于Action方法过滤器。

以下示例是同步 Action 过滤器调用的顺序:

序号 过滤器作用域 过滤器方法
1 Global OnActionExecuting
2 Controller OnActionExecuting
3 Method OnActionExecuting
4 Method OnActionExecuted
5 Controller OnActionExecuted
6 Global OnActionExecuted

提示

每个控制器的基类Controller包含OnActionExecutingOnActionExecuted方法。其中OnActionExecuting在所有过滤器之前调用,OnActionExecuted在所有过滤器之后调用。

覆盖默认执行顺序

您可以通过实现IOrderedFilter接口来覆盖默认的执行顺序。此接口公开了Order属性表示优先级,以确定执行顺序;具有较低Order值的过滤器将在具有较高Order值的过滤器之前执行前置方法;具有较低Order值的过滤器将在具有较高Order值的过滤器之后执行后置方法。

您可以使用构造函数参数设置Order属性:

[MyFilter(Name = "Controller Level Attribute", Order=1)]

如果您将上述示例中 Action 过滤器的Order设置为1,将控制器和全局过滤器的Order属性分别设置为2和3,则执行顺序将与默认相反。

序号 过滤器作用域 Order 属性 过滤器方法
1 Method 1 OnActionExecuting
2 Controller 2 OnActionExecuting
3 Global 3 OnActionExecuting
4 Global 3 OnActionExecuted
5 Controller 2 OnActionExecuted
6 Method 1 OnActionExecuted

过滤器执行时,Order属性的优先级高于作用域。过滤器首先按Order属性排序,然后再按作用域排序。所有内置过滤器实现IOrderedFilter接口并将Order值默认设置为0;因此,除非设置Order属性为非零值,否则按作用域的优先级执行。

## 总结
今天我们已经了解了关于过滤器基本知识,在下一篇博客中,我们将介绍内置过滤器、过滤的使用、依赖注入、取消与截断等知识,谢谢!

参考资料

转载请注明出处,原文链接:http://www.cnblogs.com/tdfblog/p/filters-in-aspnet-core-mvc.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,989
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,503
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,131
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,842