鍍金池/ 教程/ C#/ ASP.Net MVC過濾器
ASP.Net MVC簡介
ASP.Net MVC過濾器
ASP.Net MVC視圖
ASP.Net MVC安全
ASP.Net MVC手腳架
ASP.Net MVC控制器
ASP.Net MVC與SQL Server數(shù)據(jù)庫操作
ASP.Net MVC NuGet包管理
ASP.Net MVC入門程序
ASP.Net MVC Razor
ASP.Net MVC Bootstrap
ASP.Net MVC單元測試
ASP.Net MVC動作
ASP.Net MVC模式
ASP.Net MVC選擇器
ASP.Net MVC開發(fā)環(huán)境配置
ASP.Net MVC生命周期
ASP.Net MVC模型綁定
ASP.Net MVC自托管(本地主機(jī)部署)
ASP.Net MVC驗證
ASP.Net MVC緩存
ASP.Net MVC數(shù)據(jù)模型
ASP.Net MVC路由
ASP.Net MVC教程
ASP.Net MVC助手
ASP.Net MVC數(shù)據(jù)注解
ASP.Net MVC Web API

ASP.Net MVC過濾器

在ASP.NET MVC中,控制器定義的操作方法通常與可能的用戶交互具有一對一的關(guān)系,但有時候您希望在調(diào)用操作方法之前或者在操作方法運(yùn)行之后執(zhí)行邏輯。

為了支持這個功能,ASP.NET MVC提供了過濾器。 過濾器是一個自定義類,它提供了一種聲明式和程序式的方法,可將操作前和操作后行為添加到控制器操作方法中。

動作過濾器

動作過濾器是一個屬性,可以將其應(yīng)用于控制器動作或整個控制器,以修改執(zhí)行動作的方式。 ASP.NET MVC框架包含幾個操作過濾器 -

  • OutputCache - 將控制器操作的輸出緩存指定的時間量。
  • HandleError - 處理執(zhí)行控制器操作時引發(fā)的錯誤。
  • Authorize - 使能夠限制對特定用戶或角色的訪問。

過濾器的類型

ASP.NET MVC框架支持四種不同類型的過濾器 -

  • 授權(quán)過濾器 - 實現(xiàn)IAuthorizationFilter屬性。
  • 動作過濾器 - 實現(xiàn)IActionFilter屬性。
  • 結(jié)果過濾器 - 實現(xiàn)IResultFilter屬性。
  • 異常過濾器 - 實現(xiàn)IExceptionFilter屬性。

過濾器按上面列出的順序執(zhí)行。例如,授權(quán)過濾器始終在每個其他類型的過濾器之后執(zhí)行操作過濾器和異常過濾器之前執(zhí)行。

授權(quán)過濾器 用于實現(xiàn)控制器操作的身份驗證和授權(quán)。 例如,授權(quán)過濾器是授權(quán)過濾器的一個例子。

下面來看一個簡單的例子,創(chuàng)建一個新的ASP.Net MVC項目。打開Visual Studio,然后單擊菜單:文件 -> 新建 -> 項目 選項。創(chuàng)建一個名稱為:MVCFiltersDemo 的MVC項目。

詳細(xì)創(chuàng)建過程請參考:http://www.yiibai.com/asp.net_mvc/asp.net_mvc_getting_started.html

通過在解決方案資源管理器 中右鍵單擊 Controllers 文件夾來添加一個控件器:HomeController。在彈出菜單項中選擇:添加 -> 控制器 。

應(yīng)用操作篩選器

動作過濾器可以應(yīng)用于單獨(dú)的控制器動作或整個控制器。 例如,操作篩選器OutputCache應(yīng)用于名為Index()的操作,該操作返回字符串。 此過濾器會將該操作返回的值緩存15秒。

為了使這個實現(xiàn)這個例子,讓我們修改控制器類通過改變稱為Index操作方法使用下面的代碼 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        // GET: Home
        [OutputCache(Duration = 15)]
        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }
    }
}

運(yùn)行此應(yīng)用程序時,將看到瀏覽器正在顯示Index操作方法的結(jié)果,如下所示 -

再添加另一個操作方法,它用于顯示當(dāng)前時間 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [OutputCache(Duration = 15)]
        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }
        // 返回當(dāng)前時間
        [OutputCache(Duration = 20)]
        public string GetCurrentTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}

請求以下URL:http://localhost:54713/Home/GetCurrentTime,將收到以下輸出 -

如果刷新瀏覽器,則會看到相同的時間,因為該操作被緩存了20秒。 20秒后刷新它將被更新。

自定義過濾器

要創(chuàng)建自己的自定義過濾器,ASP.NET MVC框架提供了一個叫作ActionFilterAttribute的基類。 這個類實現(xiàn)了IActionFilterIResultFilter接口,都是從Filter類派生的。

下面來看看看自定義過濾器的一個簡單的例子,通過在項目中創(chuàng)建一個新的文件夾:ActionFilters 。添加一個類,右鍵單擊ActionFilters 文件夾并選擇:添加 -> 。

在類名稱字段中輸入MyLogActionFilter,然后單擊“添加” 按鈕。

這個類將從ActionFilterAttribute派生,它是一個基類,并覆蓋下面的方法。 以下是MyLogActionFilter的完整實現(xiàn)。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MVCFiltersDemo.ActionFilters
{
    public class MyLogActionFilter: ActionFilterAttribute
    {        

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Log("##########OnActionExecuted", filterContext.RouteData);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            Log("##########OnResultExecuting", filterContext.RouteData);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            Log("##########OnResultExecuted", filterContext.RouteData);
        }

        private void Log(string methodName, RouteData routeData)
        {
            var controllerName = routeData.Values["controller"];
            var actionName = routeData.Values["action"];

            var message = String.Format(
               "{0} controller:{1} action:{2}", methodName, controllerName, actionName);
            System.Console.WriteLine("############################# YES ##################");
            Debug.WriteLine(message, "Action Filter Log");
        }
    }

}

現(xiàn)在,使用以下代碼將日志過濾器應(yīng)用于HomeController控制器。

using MVCFiltersDemo.ActionFilters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    [MyLogActionFilter]
    public class HomeController : Controller
    {
        // GET: Home
        [OutputCache(Duration = 10)]
        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }

        // 返回當(dāng)前時間
        [OutputCache(Duration = 10)]
        public string GetCurrentTime()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
    }
}

打開項目中的App_Start,打開文件FilterConfig.cs ,添加以下代碼注冊過濾器:MyLogActionFilter -

using MVCFiltersDemo.ActionFilters;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            // 注冊自定義Action過濾器:優(yōu)先級最低,但是可以作用到所有的控制器和Action  
            filters.Add(new MyLogActionFilter());
        }
    }
}

運(yùn)行應(yīng)用程序,然后觀察輸出窗口。應(yīng)該會看到類似(輸出的最后幾行)的結(jié)果 -

如上圖所示,處理動作的每個階段都有被記錄到Visual Studio輸出窗口中了。


上一篇:ASP.Net MVC簡介