zoukankan      html  css  js  c++  java
  • MVC过滤器

    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之后执行

  • 相关阅读:
    高并发场景 LVS 安装及keepalived的应用
    使用nginx作为http/https正向代理
    Spring5【七】Spring 整合 MyBatis
    Spring5【六】代理模式及 AOP
    MyBatis 配置模板
    Spring5【五】Bean 的自动装配及注解开发
    Spring5【四】依赖注入(DI)
    Spring5【三】IoC 创建对象的方式及配置说明
    Spring5【一】Spring 简介
    MyBatis【七】缓存
  • 原文地址:https://www.cnblogs.com/Jack-Blog/p/4058112.html
Copyright © 2011-2022 走看看