关于.net:如何在-ASPNET-Core-中使用-ActionFilter

11次阅读

共计 2590 个字符,预计需要花费 7 分钟才能阅读完成。

ASP.NET Core MVC 中的 Filters 容许咱们在 申请解决管道 中的某一个阶段的之前和之后执行自定义代码,不同类型的 filter 对应着 申请解决管道 的不同阶段,比如说:ActionFilter 能够在 Action 办法的之前或者之后执行自定义代码,这篇文章咱们就来探讨 ASP.NET Core MVC 中内建的 ActionFilter,为什么它十分有用以及在程序中如何应用它。

Filter 过滤器

其实在 ASP.NET Core MVC 中有很多的内建 filter,大体列举如下:

  • ActionFilters

它会在 Action 办法的执行前和执行后 执行。

  • AuthorizationFilters

它会在 申请解决管道 的开始处被执行,次要用来获取用户的 凭证信息 来验证用户是否被受权。

  • ResourceFilters

它会在 authorization 之后 和 模型绑定 之前被执行,能够利用它实现一些缓存逻辑。

  • ExceptionFilters

它会捕捉到 申请解决管道 中的所有异样,所以可用它来实现一些自定义的异样解决。

到底用哪一种类型的 filter,还是取决于你到底想实现什么业务,举个例子,如果你想 短路 request,提前结束 pipeline 管道返回后果,是不是就能够用 ResourceFilters 哈,再举一个例子,如果你想批改 Action 的入参 并且想对 Action 的后果进行批改,那么 ActionFilter 就是你的最佳抉择。

ASP.NET Core MVC 中有一个个性叫 ActionFilterAttribute,它实现了如下接口 IActionFilter, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter,能够利用它实现不同层级的 Filter,如:Action 级,Controller 级,全局级,稍后咱们将会一一探讨。

创立自定义的 ActionFilter

你能够利用自定义的 ActionFilter 在 Action 办法的前后执行一些可复用的逻辑,或者大家都晓得,这就是所谓的 AOP 编程,除了 ActionFilterAttribute,还有其余几个 Filter 也有相似的 Attribute。

  • ResultFilterAttribute
  • ExceptionFilterAttribute
  • ServiceFilterAttribute
  • TypeFilterAttribute

除了下面这些快捷个性,最简略粗犷的就是实现 IActionFilter 接口,还能够实现 同步 异步 双模式。

创立同步的 ActionFilter

上面的代码片段展现了如何创立同步模式的 ActionFilter,继承 IActionFilter 接口并实现它的 OnActionExecutingOnActionExecuted 两个办法。


    public class SimpleActionFilter : IActionFilter
    {public void OnActionExecuting(ActionExecutingContext context)
        {//this method will be executed before execution of an action method}
        public void OnActionExecuted(ActionExecutedContext context)
        {//this method will be executed after an action method has executed}
    }

创立异步模式的 ActionFilter

上面的代码片段展现了如何创立异步模式的 ActionFilter,继承 IAsyncActionFilter 接口并实现它的 OnActionExecutionAsync 办法。


    public class SimpleAsyncActionFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context,
          ActionExecutionDelegate next)
        {
            //code written here will be executed before execution of an action method 
            await next();
            //code written here will be executed after execution of an action method 
        }
    }

配置 ActionFilter

文章之前也说过了,能够将 filter 过滤器 增加到不同级别的作用域中,这些作用域包含:action 级,controller 级,global 级,这里就来演示如何将 filter 增加到 global 级,仔细观察一下我的 自定义 filter 是如何增加到 ConfigureServices 办法下的 filter 汇合 中,如下代码所示:


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {options.Filters.Add(new SimpleAsyncActionFilter());
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

除了下面的办法,还能够用 typeof 的形式退出到 options 中,如下代码所示:


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(options =>
            {options.Filters.Add(typeof(SimpleAsyncActionFilter));
            }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

总结一下:过滤器容许咱们在 申请解决管道 中的某一个点的前后执行一些自定义代码,而且 ActionFilter 还有一个十分大的新改良是能够在 Http 申请管道中指定过滤器的执行程序,对于更多的 filter 的高级个性,我会在前面的文章中和大家一起分享。

译文链接:https://www.infoworld.com/art…

正文完
 0