ASP.NET MVC AOP-Filter

      软件开发模式从面向过程(POP)发展到面向对象(OOP)之后,软件模块之间的组织方式发生了很多变化,衍生出了很多可插拔的模块化组件,给软件的灵活架构方面带来了丰富多彩的内容。面相对象的思维方式可谓功不可没,在日趋复杂的应用系统中,有很多基本的业务逻辑,跟具体的业务关系很小,甚至没有关系,比如日志,参数校验,权限,等等各种常规功能,这时候我们就需要面相切面编程-AOP

ASP.NET MVC 在控制器加载及执行过程中,为了让用户能够扩展控制器执行过程中的功能,提供了让用户重OnActionExecuting,OnActionExecuted,OnResultExecuting,OnResultExecuted几个方法的机制。这样用户就可以灵活地控制Action和Result的执行过程。其实,这就是AOP在ASP.NET MVC 中最好的实现,也是ASP.NET MVC 在设计中非常具有借鉴意义的部分,为我们设计具有AOP机制的模块具有很好的借鉴意义。

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

namespace AspNetMvcFilterDemo.Filters
{
    public class MyFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("Method Name:OnActionExecuting
"); //输出请求的Controller 与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("Controller :{0}
action:{1}
", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------------------------
"); //base.OnActionExecuting(filterContext); } public override void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.HttpContext.Response.Write("Method Name:OnActionExecuted
"); //输出请求的Controller 与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("Controller :{0}
action:{1}
", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------------------------
"); //base.OnActionExecuted(filterContext); } public override void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.HttpContext.Response.Write("Method Name:OnResultExecuting
"); //输出请求的Controller 与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("Controller :{0}
action:{1}
", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------------------------
"); //base.OnResultExecuting(filterContext); } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.Write("Method Name:OnResultExecuted
"); //输出请求的Controller 与action名称 String controllerName = filterContext.RouteData.Values["controller"].ToString(); String actionName = filterContext.RouteData.Values["action"].ToString(); String message = String.Format("Controller :{0}
action:{1}
", controllerName, actionName); filterContext.HttpContext.Response.Write(message); filterContext.HttpContext.Response.Write("------------------------------------
"); //base.OnResultExecuted(filterContext); } } }
我们就可以在控制器上使用Attribute

[MyFilter]
public ActionResult Index()
{
      return View();
}
效果:

ASP.NET MVC AOP-Filter_第1张图片

你可能感兴趣的:(ASP.NET,MVC)