码迷,mamicode.com
首页 > Web开发 > 详细

MVC过滤器

时间:2014-10-28 23:41:27      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:http   io   sp   on   cti   bs   as   res   tt   

ActionFilterAttribute,里面有4个虚方法
        public virtual void OnActionExecuted(ActionExecutedContext filterContext);
        public virtual void OnActionExecuting(ActionExecutingContext filterContext);
        public virtual void OnResultExecuted(ResultExecutedContext filterContext);
        public virtual void OnResultExecuting(ResultExecutingContext filterContext);

先在Model创建一个类名为MyActionFilterAttribute,继承自ActionFilterAttribute

在里面定义Name并重载4个虚方法 加入输出提示

 public string Name { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);

            HttpContext.Current.Response.Write("<br />OnActionExecuting " + this.Name);
        }
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            HttpContext.Current.Response.Write("<br />OnResultExecuting " + this.Name);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            HttpContext.Current.Response.Write("<br />OnActionExecuting " + this.Name);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            base.OnResultExecuted(filterContext);
            HttpContext.Current.Response.Write("<br />OnResultExecuted " + this.Name);
        }

 

新建一个控制器名为FilterController 在其里面写入

[MyActionFilterAttribute(Name="Index Action")]
        public ActionResult Index()
        {
            Response.Write("<p>Action执行了</>");
            return Content("<br />试图被渲染了");
        }

进入该Filter控制器的Index  Action方法

 

显示结果:

OnActionExecuting Index Action

Action执行了
OnActionExecuting Index Action
OnResultExecuting Index Action
试图被渲染了
OnResultExecuted Index Action

说明OnActionExecuting在Action前执行

OnActionExecuting在Action后执行

OnResultExecuting在View之前执行

OnResultExecuted在View之后执行

 

MVC过滤器

标签:http   io   sp   on   cti   bs   as   res   tt   

原文地址:http://www.cnblogs.com/Jack-Blog/p/4058112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!