在ASP.NET MVC中有四種過濾器類型html
一、在ASP.NET MVC項目中,新建文件夾Filter,而後新建類MyCustormFilter,繼承自ActionFilterAttribute類,咱們來看下ActionFilterAttribute類有以下四個方法,從命名我應該就能夠看出他們的執行時機。asp.net
public class MyCustormFilter:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.HttpContext.Response.Write("Action執行前"); base.OnActionExecuting(filterContext); } public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("Action執行後"); base.OnActionExecuted(filterContext); } }
二、對於過濾器,咱們能夠把它們加在三個地方,一個是控制器上面(控制器下面的全部Action),一個是Action上面(指定標識的Action),另外一個就是全局位置(全部控制器中的Action)。這裏我只演示在Action上面加ide
[MyCustormFilter]
public ActionResult Index()
{
return View();
}
public ActionResult Index1()
{
return View();
}
三、build而後運行ui
一、新建類TestResultFilter,繼承ActionFilterAttributespa
public class TestResultFilter:ActionFilterAttribute { /// <summary> /// 加載 "視圖" 前執行 /// </summary> /// <param name="filterContext"></param> public override void OnResultExecuting(System.Web.Mvc.ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("加載視圖前執行 OnResultExecuting <br/>"); base.OnResultExecuting(filterContext); } /// <summary> /// 加載"視圖" 後執行 /// </summary> /// <param name="filterContext"></param> public override void OnResultExecuted(System.Web.Mvc.ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("加載視圖後執行 OnResultExecuted <br/>"); base.OnResultExecuted(filterContext); } }
二、這裏我把TestResultFilter過濾器加在控制器上面.net
[TestResultFilter] public class FilterTestController : Controller { [MyCustormFilter] public ActionResult Index() { return View(); } public ActionResult Index1() { return View(); } }
三、運行看下結果3d
注意:Result過濾器,不管Action的返回類型爲何(甚至void)都將執行。日誌
RouteData中保存了當前請求匹配的路由信息和路由對象code
修改MyCustormFilter.csorm
public override void OnActionExecuting(ActionExecutingContext filterContext) { //1.獲取獲取請求的類名和方法名 string strController = filterContext.RouteData.Values["controller"].ToString(); string strAction = filterContext.RouteData.Values["action"].ToString();//2.另外一種方式 獲取請求的類名和方法名 string strAction2 = filterContext.ActionDescriptor.ActionName; string strController2 = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; filterContext.HttpContext.Response.Write("Action執行前</br>"); filterContext.HttpContext.Response.Write("控制器:" + strController + "</br>"); filterContext.HttpContext.Response.Write("控制器:" + strController2+"</br>"); filterContext.HttpContext.Response.Write("Action:" + strAction + "</br>"); filterContext.HttpContext.Response.Write("Action:" + strAction2 + "</br>"); base.OnActionExecuting(filterContext); }
一、 新建TestAuthorizeAttribute
/// <summary> /// 受權過濾器 --在Action過濾器前執行 /// </summary> public class TestAuthorizeAttribute:AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { filterContext.HttpContext.Response.Write("<br/>OnAuthorization<br/>"); //註釋掉父類方法,由於父類裏的 OnAuthorization 方法會調用asp.net的受權驗證機制! //base.OnAuthorization(filterContext); } }
二、在控制器FilterTest中的Index上添加TestAuthorize標記
[MyCustormFilter] [TestAuthorize] public ActionResult Index() { return View(); }
運行看下結果:
一、新建TestHandleError.cs
/// <summary> /// 異常處理 過濾器 /// </summary> public class TestHandleError : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { //1.獲取異常對象 Exception ex = filterContext.Exception; //2.記錄異常日誌 //3.重定向友好頁面 filterContext.Result = new RedirectResult("~/error.html"); //4.標記異常已經處理完畢 filterContext.ExceptionHandled = true; base.OnException(filterContext); } }
二、在Action上面加TestHandleError
[TestHandleError] public ActionResult GetErr() { int a = 0; int b = 1 / a; return View(); }
注意:一般這樣的異常處理咱們是放在全局過濾器上面的。
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { //filters.Add(new HandleErrorAttribute()); //添加全局過濾器 filters.Add(new TestHandleError()); } }