zoukankan      html  css  js  c++  java
  • Asp.Net Mvc Action过滤器(二)

    在Mvc中为Action添加过滤器,有两种方式,

    一、使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理,

    1.可以为空,支持的重写:OnActionExecuted,OnActionExecuting,OnResultExecuted,OnResultExecuting

    2.支持类定义或方法定义

    3.不支持多个过滤器实例,我的理解是一个action只能指定一个过滤器,目前还没有验证。

        //
        // 摘要:
        //     表示筛选器特性的基类。
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
        public abstract class ActionFilterAttribute : FilterAttribute, IActionFilter, IResultFilter

    空使用实例:

    public class AboveLogin2Attibute : ActionFilterAttribute
    {
    
    }

    二、使用IActionFilter接口

    1.专门用来处理Action的过滤处理

    2.因为是接口,必须实现OnActionExecuted和OnActionExecuting

    3.此方式可以通过FilterAttribute,指定执行顺序,配置是否可指定筛选器特性的多个实例

        //
        // 摘要:
        //     定义操作筛选器中使用的方法。
        public interface IActionFilter
        {
            //
            // 摘要:
            //     在执行操作方法后调用。
            //
            // 参数:
            //   filterContext:
            //     筛选器上下文。
            void OnActionExecuted(ActionExecutedContext filterContext);
            //
            // 摘要:
            //     在执行操作方法之前调用。
            //
            // 参数:
            //   filterContext:
            //     筛选器上下文。
            void OnActionExecuting(ActionExecutingContext filterContext);
        }

    空使用实例:

    public class AboveLoginAttibute : FilterAttribute, IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            throw new NotImplementedException();
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            throw new NotImplementedException();
        }
    }

    更多参考:

    MVC4 过滤器(转)

    Action过滤器使用实例(一)

  • 相关阅读:
    leetcode-Single Number
    设计模式六大原则(4)——接口隔离原则
    设计模式六大原则(3)——依赖倒置原则
    设计模式六大原则(2)——里氏替换原则
    设计模式六大原则(1)——单一职责原则
    观察者模式
    转:画图工具
    android 博客列表
    app crash率的标准
    查看某一个开发者代码修改量的脚本(ios平台可用)
  • 原文地址:https://www.cnblogs.com/tianma3798/p/6346588.html
Copyright © 2011-2022 走看看