mvc Action Filter
mvc Action Filter中OnActionExecuting、OnActionExecuted、OnResultExecuting、OnResultExecuted的执行顺序
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO; namespace MvcApp.ActionFilters { public class SimpleFilterAttribute:ActionFilterAttribute { public int ID { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext) { File.AppendAllText(@"D:\ActionFilterTest.log", "SimpleFilter " + this.ID + " OnActionExecuting! \r\n"); } public override void OnActionExecuted(ActionExecutedContext filterContext) { File.AppendAllText(@"D:\ActionFilterTest.log", "SimpleFilter " + this.ID + " OnActionExecuted! \r\n"); } public override void OnResultExecuting(ResultExecutingContext filterContext) { File.AppendAllText(@"D:\ActionFilterTest.log", "SimpleFilter " + this.ID + " OnResultExecuting! \r\n"); } public override void OnResultExecuted(ResultExecutedContext filterContext) { File.AppendAllText(@"D:\ActionFilterTest.log", "SimpleFilter " + this.ID + " OnResultExecuted! \r\n"); } } }
[SimpleFilter(ID = 1, Order = 1)]
public ActionResult Index()
{
ViewData["Message"] = "Get the Filter Data!";
return View();
}
这样的执行的结果是
OnActionExecuted可以处理执行过程中的异常
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApp.ActionFilters { public class ErrorHandlingFilterAttribute:ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Exception != null) { filterContext.ExceptionHandled = true; //Filter是Controllers的名称 filterContext.Result = new RedirectToRouteResult("Filter", new RouteValueDictionary(new { action = "OnError" })); } } } }
新建一个OnError的视图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public ActionResult OnError() { ViewData["Title"] = "OnError"; ViewData["Message"] = "Error Page!"; return View(); } [ErrorHandlingFilter] public void RaiseError() { throw new Exception(); }
OnResultExecuting可以用于服务器端的压缩和客户端缓存
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO.Compression; namespace MvcApp.ActionFilters { public class CompressFilterAttribute:ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { string accpetEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; if (string.IsNullOrEmpty(accpetEncoding)) return; var response = filterContext.HttpContext.Response; accpetEncoding=accpetEncoding.ToUpperInvariant(); if (accpetEncoding.Contains("GZIP")) { response.AddHeader("content-encoding", "gzip"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } else if (accpetEncoding.Contains("DEFLATE")) { response.AddHeader("content-encoding", "deflate"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } } } }
新建一个Compress的视图,可以压缩前后的大小
[CompressFilter]
public ActionResult Compress()
{
return View();
}
OnResultExecuting可以用于页面尾部输出信息和异常处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApp.ActionFilters { public class TraceFilterAttribute:ActionFilterAttribute { public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Items["Start"] = DateTime.Now; } public override void OnResultExecuted(ResultExecutedContext filterContext) { DateTime start = (DateTime)filterContext.HttpContext.Items["Start"]; TimeSpan ts = DateTime.Now - start; filterContext.HttpContext.Response.Write("<hr />" + ts.ToString()); } } }
新建一个Timing的视图
[TraceFilter]
public ActionResult Timing()
{
Random rdm = new Random(DateTime.Now.Millisecond);
Thread.Sleep(rdm.Next(5) * 3000);
return View();
}
posted on 2014-12-05 14:41 lovezj9012 阅读(194) 评论(0) 编辑 收藏 举报