如何在 ASP.NET Core 中使用 ActionFilter
ASP.NET Core MVC 中的 Filters 允許我們?cè)?請(qǐng)求處理管道 中的某一個(gè)階段的之前和之后執(zhí)行自定義代碼,不同類(lèi)型的 filter 對(duì)應(yīng)著 請(qǐng)求處理管道 的不同階段,比如說(shuō):ActionFilter 可以在 Action 方法的之前或者之后執(zhí)行自定義代碼,這篇文章我們就來(lái)討論 ASP.NET Core MVC 中內(nèi)建的 ActionFilter,為什么它非常有用以及在程序中如何使用它。
Filter 過(guò)濾器
其實(shí)在 ASP.NET Core MVC 中有很多的內(nèi)建 filter,大體羅列如下:
ActionFilters
它會(huì)在 Action 方法的執(zhí)行前和執(zhí)行后 執(zhí)行。
AuthorizationFilters
它會(huì)在 請(qǐng)求處理管道 的開(kāi)始處被執(zhí)行,主要用來(lái)獲取用戶(hù)的 憑證信息 來(lái)驗(yàn)證用戶(hù)是否被授權(quán)。
ResourceFilters
它會(huì)在 authorization 之后 和 模型綁定 之前被執(zhí)行,可以利用它實(shí)現(xiàn)一些緩存邏輯。
ExceptionFilters
它會(huì)捕捉到 請(qǐng)求處理管道 中的所有異常,所以可用它來(lái)實(shí)現(xiàn)一些自定義的異常處理。
到底用哪一種類(lèi)型的 filter,還是取決于你到底想實(shí)現(xiàn)什么業(yè)務(wù),舉個(gè)例子,如果你想 短路 request,提前結(jié)束 pipeline 管道返回結(jié)果,是不是就可以用 ResourceFilters 哈,再舉一個(gè)例子,如果你想修改 Action 的入?yún)?并且想對(duì) Action 的結(jié)果進(jìn)行修改,那么 ActionFilter 就是你的最佳選擇。
ASP.NET Core MVC 中有一個(gè)特性叫 ActionFilterAttribute,它實(shí)現(xiàn)了如下接口 IActionFilter, IAsyncActionFilter, IResultFilter, IAsyncResultFilter, IOrderedFilter,可以利用它實(shí)現(xiàn)不同層級(jí)的Filter,如:Action級(jí),Controller級(jí),全局級(jí),稍后我們將會(huì)一一討論。
創(chuàng)建自定義的 ActionFilter
你可以利用自定義的 ActionFilter 在 Action 方法的前后執(zhí)行一些可復(fù)用的邏輯,或許大家都知道,這就是所謂的 AOP 編程,除了 ActionFilterAttribute ,還有其他幾個(gè) Filter 也有類(lèi)似的 Attribute。
ResultFilterAttribute
ExceptionFilterAttribute
ServiceFilterAttribute
TypeFilterAttribute
除了上面這些快捷特性,最簡(jiǎn)單粗暴的就是實(shí)現(xiàn) IActionFilter 接口 ,還可以實(shí)現(xiàn) 同步 和 異步 雙模式。
創(chuàng)建同步的 ActionFilter
下面的代碼片段展示了如何創(chuàng)建同步模式的 ActionFilter,繼承 IActionFilter 接口并實(shí)現(xiàn)它的 OnActionExecuting 和 OnActionExecuted 兩個(gè)方法。
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?}}創(chuàng)建異步模式的 ActionFilter
下面的代碼片段展示了如何創(chuàng)建異步模式的 ActionFilter,繼承 IAsyncActionFilter 接口并實(shí)現(xiàn)它的 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
文章之前也說(shuō)過(guò)了,可以將 filter 過(guò)濾器 添加到不同級(jí)別的作用域中,這些作用域包括:action級(jí), controller級(jí),global級(jí),這里就來(lái)演示如何將 filter 添加到 global級(jí) ,仔細(xì)觀察一下我的 自定義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);}總結(jié)一下:過(guò)濾器允許我們?cè)?請(qǐng)求處理管道 中的某一個(gè)點(diǎn)的前后執(zhí)行一些自定義代碼,而且 ActionFilter 還有一個(gè)非常大的新改進(jìn)是可以在 Http 請(qǐng)求管道中指定過(guò)濾器的執(zhí)行順序,關(guān)于更多的 filter 的高級(jí)特性,我會(huì)在后面的文章中和大家一起分享。
譯文鏈接:https://www.infoworld.com/article/3328648/how-to-use-action-filters-in-aspnet-core-mvc.html
總結(jié)
以上是生活随笔為你收集整理的如何在 ASP.NET Core 中使用 ActionFilter的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 引入Jaeger——使用
- 下一篇: 【源码解读】Vue与ASP.NET Co