MVC过滤器Attribute
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 />OnActionExecuted" + 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执行了
OnActionExecuted Index Action
OnResultExecuting Index Action
试图被渲染了
OnResultExecuted Index Action
说明OnActionExecuting在Action前执行
OnActionExecuted在Action后执行
OnResultExecuting在View之前执行
OnResultExecuted在View之后执行
关注我】。(●'◡'●)
如果,您希望更容易地发现我的新博客,不妨点击一下绿色通道的【因为,我的写作热情也离不开您的肯定与支持,感谢您的阅读,我是【Jack_孟】!
本文来自博客园,作者:jack_Meng,转载请注明原文链接:https://www.cnblogs.com/mq0036/p/12748596.html
【免责声明】本文来自源于网络,如涉及版权或侵权问题,请及时联系我们,我们将第一时间删除或更改!