<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ASP.NET Core Filter如何支持依賴注入

          共 3446字,需瀏覽 7分鐘

           ·

          2021-03-28 16:06


          概述

          通過使用 ASP.NET Core 中的篩選器,可在請求處理管道中的特定階段之前或之后運(yùn)行代碼。內(nèi)置篩選器處理任務(wù),例如:授權(quán)(防止用戶訪問未獲授權(quán)的資源)。響應(yīng)緩存(對請求管道進(jìn)行短路出路,以便返回緩存的響應(yīng))。可以創(chuàng)建自定義篩選器,用于處理橫切關(guān)注點。 橫切關(guān)注點的示例包括錯誤處理、緩存、配置、授權(quán)和日志記錄。 篩選器可以避免復(fù)制代碼。 例如,錯誤處理異常篩選器可以合并錯誤處理。


          ASP.NET Core Filter如何支持依賴注入?可以通過全局注冊,支持依賴注入。通過TypeFilter(typeof(Filter)) 標(biāo)記在方法,標(biāo)記在控制器。通過ServiceType(typeof(Filter))標(biāo)記在方法,標(biāo)記在控制器,必須要注冊Filter這類;TypeFilter和ServiceType的本質(zhì)是實現(xiàn)了一個IFilterFactory接口;

          代碼實現(xiàn)

          1、普通Filter使用,繼承: Attribute, IActionFilter

           public class TestActionFilterAttribute : Attribute, IActionFilter    {        public void OnActionExecuted(ActionExecutedContext context)        {            if (context.HttpContext.Request.Query.TryGetValue("id", out StringValues value))            {                Console.WriteLine(value.First());            }            else            {                context.HttpContext.Response.Redirect("/Error/404");            }        }
          public void OnActionExecuting(ActionExecutingContext context) { } }
             [TestActionFilter]        public IActionResult Index()        {            return View();        }

          2、使用  [TypeFilter(typeof(TestActionFilterAttribute))]注入。

            public class TestActionFilterAttribute : Attribute, IActionFilter    {        private readonly ILogger _logger;
          public TestActionFilterAttribute(ILoggerFactory logger) { _logger = logger.CreateLogger("TestActionFilterAttribute"); }
          public void OnActionExecuted(ActionExecutedContext context) { _logger.LogDebug($"11111");
          if (context.HttpContext.Request.Query.TryGetValue("id", out StringValues value)) { Console.WriteLine(value.First()); } else { context.HttpContext.Response.Redirect("/Error/404"); } }
          public void OnActionExecuting(ActionExecutingContext context) { } }
            [TypeFilter(typeof(TestActionFilterAttribute))]        public IActionResult Index()        {            return View();        }



          3、使用 [ServiceFilter(typeof(TestActionFilterAttribute))]注入。

            [ServiceFilter(typeof(TestActionFilterAttribute))]        public IActionResult Index()        {            return View();        }

          運(yùn)行測試,發(fā)現(xiàn)報錯

          于是ConfigureServices加上services.AddSingleton<TestActionFilterAttribute>();

            // This method gets called by the runtime. Use this method to add services to the container.        public void ConfigureServices(IServiceCollection services)        {            services.AddSingleton<TestActionFilterAttribute>();
          services.AddControllersWithViews(); }

          運(yùn)行測試,成功。

          4、通過全局注冊

             public void ConfigureServices(IServiceCollection services)        {            // services.AddSingleton<TestActionFilterAttribute>();
          services.AddControllersWithViews(options => { // 添加全局異常 options.Filters.Add<TestActionFilterAttribute>(); }); }


          代碼地址:

          https://gitee.com/conanOpenSource_admin/service-filter_-type-filter


          往期精彩回顧




          【推薦】.NET Core開發(fā)實戰(zhàn)視頻課程 ★★★

          .NET Core實戰(zhàn)項目之CMS 第一章 入門篇-開篇及總體規(guī)劃

          【.NET Core微服務(wù)實戰(zhàn)-統(tǒng)一身份認(rèn)證】開篇及目錄索引

          Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)

          .NET Core中的一個接口多種實現(xiàn)的依賴注入與動態(tài)選擇看這篇就夠了

          10個小技巧助您寫出高性能的ASP.NET Core代碼

          用abp vNext快速開發(fā)Quartz.NET定時任務(wù)管理界面

          在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務(wù)輕松實現(xiàn)作業(yè)調(diào)度

          現(xiàn)身說法:實際業(yè)務(wù)出發(fā)分析百億數(shù)據(jù)量下的多表查詢優(yōu)化

          關(guān)于C#異步編程你應(yīng)該了解的幾點建議

          C#異步編程看這篇就夠了


          瀏覽 50
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  人人插人人插 | 久久久一区二区三区四曲免费听 | 免费看美女黄片操逼视频 | 波多一区 | 影音先锋资源av 尤物视频最新网址 |