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过滤器使用实例(一)

  • 相关阅读:
    【前端】常用总结(二)
    【Golang】爬虫笔记
    ubuntu16.04安装SSH服务
    windows下配置pytorch环境
    使用VNC连接ubuntu16.4错误Authentication Failure问题
    window使用VNC远程ubuntu16.04
    ubuntu16.04安装nvidia显卡驱动
    python批量读取并显示图片,处理异常。
    将nii文件CT图像更改窗宽窗位之后保存成nii文件
    yaml.load与yaml.dump的用法
  • 原文地址:https://www.cnblogs.com/tianma3798/p/6346588.html
Copyright © 2011-2022 走看看