ASP.NET Core MVC中的Filter作用是在请求处理管道的某些阶段之前或之后可以运行特定的代码。

Filter特性在之前的ASP.NET MVC中已经出现,但过去只有Authorization,Exception,Action,Result四种类型,现在又增加了一种Resource类型。所以共计五种。

Resource类型Filter在Authorization类型Filter之后执行,但又在其它类型的Filter之前。且执行顺序也在Model Binding之前,所以可以对Model Binding产生影响。

ASP.NET Core MVC框架中可以看到有ConsumesAttribute及FormatFilter两种实现IResourceFilter接口的类。

ConsumesAttribute会按请求中的Content-Type(内容类型)进行过滤,而FormatFilter能对路由或路径中设置了format值的请求作过滤。

一旦不符合要求,就对ResourceExecutingContext的Result属性设置,这样可以达到短路效果,阻止进行下面的处理。

ConsumesAttribute类的例子:

public void OnResourceExecuting(ResourceExecutingContext context)
{
... // Only execute if the current filter is the one which is closest to the action.
// Ignore all other filters. This is to ensure we have a overriding behavior.
if (IsApplicable(context.ActionDescriptor))
{
var requestContentType = context.HttpContext.Request.ContentType; // Confirm the request's content type is more specific than a media type this action supports e.g. OK
// if client sent "text/plain" data and this action supports "text/*".
if (requestContentType != null && !IsSubsetOfAnyContentType(requestContentType))
{
context.Result = new UnsupportedMediaTypeResult();
}
}
}

Filter在ASP.NET Core MVC里除了保留原有的包含同步方法的接口,现在又增加了包含异步方法的接口。

同步

  • IActionFilter
  • IAuthorizationFilter
  • IExceptionFilter
  • IResourceFilter
  • IResultFilter

异步

  • IAsyncActionFilter
  • IAsyncAuthorizationFilter
  • IAsyncExceptionFilter
  • IAsyncResourceFilter
  • IAsyncResultFilter

新的接口不像旧有的接口包含两个同步方法,它们只有一个异步方法。但可以实现同样的功能。

public class SampleAsyncActionFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(
ActionExecutingContext context,
ActionExecutionDelegate next)
{
// 在方法处理前执行一些操作
var resultContext = await next();
// 在方法处理后再执行一些操作。
}
}

Attribute形式的Filter,其构造方法里只能传入一些基本类型的值,例如字符串:

public class AddHeaderAttribute : ResultFilterAttribute
{
private readonly string _name;
private readonly string _value; public AddHeaderAttribute(string name, string value)
{
_name = name;
_value = value;
} public override void OnResultExecuting(ResultExecutingContext context)
{
context.HttpContext.Response.Headers.Add(
_name, new string[] { _value });
base.OnResultExecuting(context);
}
} [AddHeader("Author", "Steve Smith @ardalis")]
public class SampleController : Controller

如果想要在其构造方法里引入其它类型的依赖,现在可以使用ServiceFilterAttribute,TypeFilterAttribute或者IFilterFactory方式。

ServiceFilterAttribute需要在DI容器中注册:

public class GreetingServiceFilter : IActionFilter
{
private readonly IGreetingService greetingService; public GreetingServiceFilter(IGreetingService greetingService)
{
this.greetingService = greetingService;
} public void OnActionExecuting(ActionExecutingContext context)
{
context.ActionArguments["param"] =
this.greetingService.Greet("James Bond");
} public void OnActionExecuted(ActionExecutedContext context)
{ }
} services.AddScoped<GreetingServiceFilter>(); [ServiceFilter(typeof(GreetingServiceFilter))]
public IActionResult GreetService(string param)

TypeFilterAttribute则没有必要:

public class GreetingTypeFilter : IActionFilter
{
private readonly IGreetingService greetingService; public GreetingTypeFilter(IGreetingService greetingService)
{
this.greetingService = greetingService;
} public void OnActionExecuting(ActionExecutingContext context)
{
context.ActionArguments["param"] = this.greetingService.Greet("Dr. No");
} public void OnActionExecuted(ActionExecutedContext context)
{ }
} [TypeFilter(typeof(GreetingTypeFilter))]
public IActionResult GreetType1(string param)

IFilterFactory也是不需要的:

public class GreetingFilterFactoryAttribute : Attribute, IFilterFactory
{
public bool IsReusable => false; public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
var logger = (IGreetingService)serviceProvider.GetService(typeof(IGreetingService));
return new GreetingFilter(logger);
} private class GreetingFilter : IActionFilter
{
private IGreetingService _greetingService;
public GreetingFilter(IGreetingService greetingService)
{
_greetingService = greetingService;
}
public void OnActionExecuted(ActionExecutedContext context)
{
} public void OnActionExecuting(ActionExecutingContext context)
{
context.ActionArguments["param"] = _greetingService.Greet("Dr. No");
}
}
} [GreetingFilterFactory]
public IActionResult GreetType1(string param)

Filter有三种范围:

  • Global
  • Controller
  • Action

后两种可以通过Attribute的方式附加到特定Action方法或者Controller类之上。对于Global,则要在ConfigureServices方法内部添加。

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
// by instance
options.Filters.Add(new AddDeveloperResultFilter("Tahir Naushad")); // by type
options.Filters.Add(typeof(GreetDeveloperResultFilter));
}); }

顾名思义,Global将对所有Controller及Action产生影响。所以务必对其小心使用。

这三种范围的执行顺序在设计程序的时候也需要多作考虑:

  1. Global范围的前置处理代码
  2. Controller范围的前置处理代码
  3. Action范围的前置处理代码
  4. Action范围的后置处理代码
  5. Controller范围的后置处理代码
  6. Global范围的后置处理代码

典型的前置处理代码如常见的OnActionExecuting方法,而常见的后置处理代码,则是像OnActionExecuted方法这般的。

最新文章

  1. Linux Shell常用快捷键
  2. 个人js类库mycool
  3. JAVA基础知识之JVM-——动态代理(AOP)
  4. iOS通过openURL打开原生应用与页面(包括电话,短信,safari等)
  5. 【转】Android源代码查看途径
  6. OC基础 内存管理
  7. SQL中去除某字段中的某个字符语法
  8. 打造简易可扩展的jQuery/CSS3 Tab菜单
  9. [图形学] Chp17 OpenGL光照和表面绘制函数
  10. linux_文件类型
  11. Oracle数据库容灾备份技术探讨
  12. 开源 , KoobooJson一款高性能且轻量的JSON框架
  13. CF1015F
  14. (转)web前端知识精简
  15. 深入jUI(DWZ)
  16. CloneZilla 恢复系统报错Waiting for device dev/disk/by-id/.....
  17. sublime2常用设置
  18. 执行“hdfs dfs -ls”时报ConnectException
  19. 获取请求 header 中指定字段的值
  20. BZOJ1968_COMMON约数研究_KEY

热门文章

  1. java.awt.headless 模式
  2. Jmeter+Ant+Jenkins搭建持续集成的接口测试(推荐 Mark)
  3. 【编码题篇】收集整理来自网络上的一些常见的 经典前端、H5面试题 Web前端开发面试题
  4. 【Android】Android的进程优先级
  5. IDEA使用笔记(七)——编辑器最大个数的设置
  6. SNFAutoupdater通用自动升级组件V2.0
  7. Java 汇编代码
  8. Laravel 的 Homestead 开发环境部署
  9. 教你一招:[转载]使用 Easy Sysprep v4 封装 Windows 7 精品
  10. tronado学习