標籤:style blog http color java io strong ar
本文轉自:http://www.cnblogs.com/kissdodog/archive/2013/01/21/2869298.html
一、自訂Filter
自訂Filter需要繼承ActionFilterAttribute抽象類別,重寫其中需要的方法,來看下ActionFilterAttribute類的方法簽名。
//表示所有操作-篩選器特性的基類。 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter { protected ActionFilterAttribute(); // 在Action執行之後由 MVC 架構調用。 public virtual void OnActionExecuted(ActionExecutedContext filterContext); // 在Action執行之前由 MVC 架構調用。 public virtual void OnActionExecuting(ActionExecutingContext filterContext); // 在執行Result後由 MVC 架構調用。 public virtual void OnResultExecuted(ResultExecutedContext filterContext); // 在執行Result之前由 MVC 架構調用。 public virtual void OnResultExecuting(ResultExecutingContext filterContext); }
因此自訂過濾器可以選擇適當的方法來重寫方可。下面來舉個簡單的例子:檢查登入狀態的過濾器,沒有登入則跳轉到登入頁
控制器代碼:
[CheckLogin] //此處為自訂屬性,要引用相應的命名空間 public ActionResult Index() { return View(); } public ActionResult Login() //此Action自動往cookie裡寫入登入資訊 { HttpCookie hcUserName = new HttpCookie("username","admin"); HttpCookie hcPassWord = new HttpCookie("password","123456"); System.Web.HttpContext.Current.Response.SetCookie(hcUserName); System.Web.HttpContext.Current.Response.SetCookie(hcPassWord); return View(); }
過濾器代碼
public class CheckLogin : ActionFilterAttribute { //在Action執行之前 亂了點,其實只是判斷Cookie使用者名稱密碼正不正確而已而已。 public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpCookieCollection CookieCollect = System.Web.HttpContext.Current.Request.Cookies;if (CookieCollect["username"] == null || CookieCollect["password"] == null) { filterContext.Result = new RedirectResult("/Home/Login"); } else { if (CookieCollect["username"].Value != "admin" && CookieCollect["password"].Value != "123456") { filterContext.Result = new RedirectResult("/Home/Login"); } } } }//本樣本貪圖方便,將要跳轉到的Action放在同一個Controller下了,如果將過濾器放到Controller類頂部,則永遠也跳不到這個LoginAction。
此過濾器實現的效果是,當使用者Cookie中使用者名稱和密碼不正確則跳轉到登入頁,注意過濾器也可以放在整個Controller類的頂部,表示該Controller下的所有Action都執行該項檢查。這樣一來,控制器裡的代碼非常漂亮,再也不用所有的Action裡都充斥著判斷登入的代碼了。
二、帶參數的自訂Filter
首先,還是按照之前添加自訂過濾器的方法,添加一個自訂過濾器,只是裡面多了一個屬性,代碼如下:
public class FilterAttribute : ActionFilterAttribute { public string Message { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { base.OnActionExecuting(filterContext); filterContext.HttpContext.Response.Write("Action執行之前" + Message + "<br />"); } public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); filterContext.HttpContext.Response.Write("Action執行之後" + Message + "<br />"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { base.OnResultExecuting(filterContext); filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); filterContext.HttpContext.Response.Write("返回Result之後" + Message + "<br />"); } }
然後在調用過濾器的時候,添加上該參數,Controller代碼如下:
[Filter(Message="劉備")] //參數給上 public ActionResult Index() { return View(); }
輸出結果如下:
如果標籤打到Controller上的話,TestFilterAttributeFilter將作用到Controller下的所有的Action。
預設情況下Action上打了某個自訂標籤後,雖然在Controller上也打上了此標籤,但它只有Action上的標籤起作用了。 補充:如果Action沒有打上該標籤,那麼Controller上的標籤便會被執行。
如果想讓Action上的標籤執行一次,然後Controller上的標籤也執行一次,那麼應該如何操作呢?
我們只需在FilterAttribute類的定義上打上標記[AttributeUsage(AttributeTargets.All, AllowMultiple = true)]即可【下面類的最上面紅色字型部分】,也就是讓其成為可以多次執行的Action。代碼如下:
[AttributeUsage(AttributeTargets.All,AllowMultiple = true)] public class FilterAttribute : ActionFilterAttribute { public string Message { get; set; } ......三、全域過濾器
有時我們想有些公用的方法需要每個Action都執行,但是又不想再每一個Controller上都打上Action標籤,怎麼辦?幸好Asp。Net MVC3帶來了一個美好的東西,全域Filter。而怎麼註冊全域Filter呢?答案就在Global.asax中。讓我們看以下代碼,我是如何將上面我們定義的TestFilterAttribute 註冊到全域Filter中。
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); //註冊全域過濾器 filters.Add(new TestFilterAttribute() { Message="全域"}); }
這樣就每個Action都會執行此過濾器,而不必每個Controller頂部都加上標籤。