ASP.NET MVC 中給我們提供了內置的過濾器,通過過濾器,我們可以在控制器內的方法前后,添加必須的業務邏輯,如權限驗證,身份驗證,錯誤處理等。
今天,我們主要介紹3個過濾器:OutputCacheAttribute,AuthorizeAttribute,HandleErrorAttribute。
我們會根據這三個內置過濾器,分別舉不同的例子進行解釋說明。
1. OutputCacheAttribute
我們先看看源碼:
可以看出OutputCacheAttribute繼承了ActionFilterAttribute,所以可以重寫ActionFilterAttribute的方法OnActionExecuting等,
類中有CacheProfile,Duration,VaryByParam幾個屬性,下面我們看一下用法。添加如下代碼:
[OutputCache(Duration=10)] public ActionResult OutputCache() { ViewData["TimeCache"] = "當前時間是:" + DateTime.Now.ToLongTimeString(); return View(); }
打開瀏覽器,可以看到,不斷刷新,每10秒就會更新一次:
當然,也可以寫在頁面上,在頁面添加代碼:
<%@ OutputCache Duration="10" VaryByParam="None" %>
如下面代碼:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ OutputCache Duration="10" VaryByParam="None" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>OutputCache</title> </head> <body> <div> <%=ViewData["TimeCache"] %> </div> </body> </html>
或者使用CacheProfile,代碼如下:
[OutputCache(CacheProfile = "testProfile")] public ActionResult OutputCache() { ViewData["TimeCache"] = "當前時間是:" + DateTime.Now.ToLongTimeString(); return View(); }
在web.config添加如下配置:
<system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="testProfile" duration="10" varyByParam="*" /> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web>
2. AuthorizeAttribute
AuthorizeAttribute類的作用,主要是為頁面指定操作用戶和角色。
我們假設以Session作為頁面管理權限,新建一個CustomAuthorize類,添加如下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcMobileDMS.App_Start { public class CustomAuthorize : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { //假設以Session作為判斷頁面權限 if (filterContext.HttpContext.Session["temp"] == null) { filterContext.HttpContext.Response.Redirect("~/dms/logon"); } base.OnAuthorization(filterContext); } } }
再在控制器中的方法添加如下代碼:
[MvcMobileDMS.App_Start.CustomAuthorize] public ActionResult Authorize() { return View(); }
即可進行權限檢查。
3. HandleErrorAttribute
HandleErrorAttribute特性主要用於處理由操作引發的錯誤。在實際操作中我們可以繼承HandleErrorAttribute類,並重寫OnException方法來進行錯誤記錄。
首先,我們新建一個錯誤頁:
public ActionResult error() { return View(); }
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>error</title> </head> <body> <div> Sorry.an error occoured. </div> </body> </html>
並添加錯誤處理類ErrorHandle繼承自HandleErrorAttribute,代碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcMobileDMS.App_Start { public class ErrorHandle : HandleErrorAttribute { public override void OnException(ExceptionContext Context) { HttpContext.Current.Response.Redirect("~/dms/error"); base.OnException(Context); } } }
在DMS控制器新增一個方法EceptionThrow,並加上過濾器ErrorHandle,代碼如下:
[MvcMobileDMS.App_Start.ErrorHandle] public ActionResult EceptionThrow() { throw new ArgumentNullException("filterContext"); }
下面,我們打開瀏覽器,輸入地址:http://localhost:7449/dms/EceptionThrow,顯示如下:
當EceptionThrow()方法拋出錯誤時,ErrorHandle過濾器觸發OnException方法,跳轉到error頁面。
由此可見,我們可以通過HandleErrorAttribute過濾器,處理各種Exception,並使用自定義方法進行處理。
以上,希望能對您有所幫助O(∩_∩)O哈哈~