1. 程式人生 > >十二個 ASP.NET Core 例子——過濾器

十二個 ASP.NET Core 例子——過濾器

implement none ssa 通過 定制化 到你 about sage err

  目錄:

  1. 過濾器介紹
  2. 過濾器類別
  3. 自定義過濾器和過濾特性
  4. 直接短路返回內容
  5. 過濾器與中間件的區別
  6. 如果要全局日誌,不要用過濾器

  官方文檔傳送門


  1.過濾器介紹

  沒有權限直接返回,資源緩存,Action執行前執行後過濾,異常定制化處理,結果定制化處理 等一些操作都可以用過濾器.而且能省去不少代碼

  官方解釋:ASP.NET Core MVC中的過濾器允許您在請求處理管道中的某些階段之前或之後運行代碼

2.過濾器類別[直接引用官方解釋]

  每個過濾器類型在過濾器管道中的不同階段執行。

  • 授權過濾器首先運行,並用於確定當前用戶是否被授權用於當前請求。如果請求未經授權,它們可能使管道短路。

  • 資源過濾器是第一個在授權後處理請求的過濾器。他們可以在過濾器管道的其余部分之前運行代碼,並在管道的其余部分完成之後運行代碼。出於性能原因,它們可用於實現緩存或以其他方式短路過濾器管道。因為它們在模型綁定之前運行,所以它們對於需要影響模型綁定的任何東西都是有用的。

  • 操作過濾器可以在調用單個操作方法之前和之後立即運行代碼。它們可用於操作傳遞到操作中的參數,並從操作返回結果。

  • 異常過濾器用於將全局策略應用於任何事情已寫入響應體之前發生的未處理異常。

  • 結果過濾器可以在執行單個操作結果之前和之後立即運行代碼。只有當action方法成功執行並且對於必須圍繞視圖或格式化程序執行的邏輯有用時,它們才會運行

  3.自定義過濾器

  自定義過濾器兩種方式.這裏單獨用action過濾器介紹

  a.繼承IActionFilter實現自己的過濾器類,並且在sartUp,mvc服務中註入. 全局都會過濾,在任意一個action執行前和執行後都會過濾一次

  IActionFilter繼承需要實現兩個方法OnActionExecuted,OnActionExecuting,分別是動作執行前和動作執行後

  First:自定義過濾器類

技術分享
    public class MyActionFilter : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            
var s = "Filter_OnActionExecuted"; } public void OnActionExecuting(ActionExecutingContext context) { var s = "Filter_OnActionExecuted"; } }
View Code

  Second:註入

技術分享
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc(options =>
            {
                options.Filters.Add(new MyActionFilter());
            });
        }
View Code

  b.繼承Attribute, IActionFilter,通過Attribute特性標識到想要過濾的地方實現局部過濾(controller,action)

  First:自定義過濾特性

技術分享
    public class MyActionFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            var s = "Attribute_OnActionExecuted";
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var s = "Attribute_OnActionExecuting";
        }
    }
View Code

  Second:標註限制單個action過濾

技術分享
        [MyActionFilter]
        public IActionResult Index()
        {
            return View();
        }
View Code

  Second:標註限制單個controller過濾

技術分享
    [MyActionFilter]
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }
View Code

註意點:當你實現自定義過濾器註入後,又實現了該類別過濾器的特性.那麽執行順序是全局過濾器OnActionExecuting,再是特性OnActionExecuting,再是特性OnActionExecuted,最後是過濾器OnActionExecuted.

  4.直接短路返回內容 

技術分享
 public class MyActionFilterAttribute : Attribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            context.Result = new ContentResult()
            {
                Content = "Resource unavailable - header should not be set"
            };
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            var s = "Attribute_OnActionExecuting";
        }
    }
View Code

  只要返回ContentResult就會短路,後面的所有邏輯不會再處理

  5.過濾器與中間件的區別

  過濾器余中間件非常的像。引用文章地址。區別在於:同作為兩個AOP利器,過濾器更貼合業務,它關註於應用程序本身,比如你看ActionFilterResultFilter,它都直接和你的Action,ActionResult交互了,是不是離你很近的感覺,那我有一些比如對我的輸出結果進行格式化啦,對我的請求的ViewModel進行數據驗證啦,肯定就是用Filter無疑了。它是MVC的一部分,它可以攔截到你Action上下文的一些信息,而中間件是沒有這個能力的。

  6.如果要全局日誌,不要用過濾器

  官方文檔中表示。建議不要用過濾器來作為日誌記錄

  原因:避免創建和使用純粹用於記錄目的的過濾器,因為內置的框架記錄功能可能已經提供您需要的。如果要將過濾器添加日誌記錄,則應將註意力集中在特定於過濾器的業務領域問題或行為上,而不是MVC操作或其他框架事件。

十二個 ASP.NET Core 例子——過濾器