1.新建一个过滤器,继承ActionFilterAttribute,然后重写

public class DemoFilterAttribute:ActionFilterAttribute
    {
        //在Action执行之前执行
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("action之前");
            base.OnActionExecuting(filterContext);
        }
        //在Action执行之后执行
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("action之后");
            base.OnActionExecuted(filterContext);
        }
        //在Result执行之前执行
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Write("Result之前");
            base.OnResultExecuting(filterContext);
        }
        //在Reuslt执行之后执行(就是方法中的代码走完之后)
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Response.Write("Result之后");
            base.OnResultExecuted(filterContext);
        }
    }

然后在要进行过滤的控制器或方法上打上该特性标签

public class AjaxDemoController : Controller
    {
        EFDemo.OAEntities db = new EFDemo.OAEntities();
        // GET: AjaxDemo
        [DemoFilter]
        public ActionResult Index()
        {
            return View();
        }
    }

2.新建一个控制器,在控制器内重写OnActionExecuting方法,然后让要进行过滤的控制器继承它

    public class BaseController : Controller
    {
        /// <summary>
        /// 在执行控制器中方法前先执行该方法
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (Session["userInfo"] == null)
            {
                //filterContext.HttpContext.Response.Redirect("/Login/Index");         filterContext.Result = Redirect("/Login/Index");
            }
        }
    }
public class UserInfoController :BaseController //Controller
    {
    }

Filter过滤的2种方式的更多相关文章

  1. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

  2. 【SpringBoot】04.SpringBoot整合Filter的两种方式

    SpringBoot整合Filter过滤器的两种方式: 1.通过扫描注解完成Filter组件注册 创建一个类,实现Filter接口,实现doFilter()方法 在该类使用注解@WebFilter,设 ...

  3. Java Enum枚举 遍历判断 四种方式(包括 Lambda 表达式过滤)

    示例代码如下: package com.miracle.luna.lambda; import java.util.Arrays; /** * @Author Miracle Luna * @Date ...

  4. nginx分发请求的2种方式:1、指明server_name;2、通过location过滤uri来分发请求;

    user nginx; worker_processes 8; # = cpu num; error_log /data/nginx/log/error/error.log warn; # warn, ...

  5. csc_滤镜filter和实现透明的两种方式

    有这样一个需求,给一个地图实现半透明效果. 使用css滤镜属性可以实现:filter. 下面是属性的所以值 filter: none | blur() | brightness() | contras ...

  6. 在Winform界面中使用DevExpress的TreeList实现节点过滤查询的两种方式

    在我较早的一篇随笔<在DevExpress程序中使用TeeList控件以及节点查询的处理>中,介绍了在树形列表TreeList控件上面,利用SearchControl实现节点的模糊查询过滤 ...

  7. spring boot 集成 Filter 的两种方式

    两种方式:(两种方式同时存在时,@Bean优先@ServletComponentScan实例化,生成两个对象) 1)@ServletComponentScan注解+@WebFilter注解 2)@Be ...

  8. 实现web数据同步的四种方式

    http://www.admin10000.com/document/6067.html 实现web数据同步的四种方式 1.nfs实现web数据共享 2.rsync +inotify实现web数据同步 ...

  9. IOS--实现滤镜效果的四种方式

    IOS–实现滤镜效果 demo地址: https://github.com/AbeDay/ios–.git 使用CIFilter来完成IOS中滤镜效果 在IOS中可以使用系统自带的方法来达到路径效果: ...

随机推荐

  1. 接口(Java)

    什么是接口:接口就是一些方法特征的集合,接口是对抽象的抽象. 在java语言中,接口有两种意思: ①概念性的接口,即系统对外提供的所有服务 ②指用interface关键字定义的接口,也称为接口类型 特 ...

  2. [CareerCup] 16.2 Measure Time in a Context Switch 测量上下文转换的时间

    16.2 How would you measure the time spent in a context switch? 上下文转换发生在两个进程之间,比如让一个等待进程进入执行和让一个运行进程进 ...

  3. [zt]系统中常用MIPS指令

    指令 功能 应用实例 LB 从存储器中读取一个字节的数据到寄存器中 LB R1, 0(R2) LH 从存储器中读取半个字的数据到寄存器中 LH R1, 0(R2) LW 从存储器中读取一个字的数据到寄 ...

  4. 制作本地 odoo deb包安装镜像

    [本来这不是个事,可是在阿里云部署的时候,这个网速真是让我无语,本来10分钟就能解决的事,得俩三个小时,太没效率了!] 原文转自 http://www.cnblogs.com/xwdreamer/p/ ...

  5. Odoo 9 Odoo $ JQuery undifned

    浏览器处于假死状态,查看console发现 odoo,jquery,$ 未定义三处错误,后台显示IOError: IOError: [Errno 2] No such file or director ...

  6. python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)

    # -*- coding:utf8 -*- import os import jieba def splitSentence(inputFile): fin = open(inputFile, 'r' ...

  7. eclipse汉化过程

    第一步: 打开浏览器,浏览“参考资料”内给出的“eclipse语言包下载”地址,在博客新页面找到地址链接,如图所示.“Babel Language...”开头的一栏下面就是各个eclise版本的语言包 ...

  8. linux命令学习(1):grep 命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  9. ADB工具和手机抓包方法介绍

    Android抓包方法 工具包内容如下:(下载地址:http://download.csdn.net/download/yezhaohui2011/8368061) adb ——谷歌提供的安卓远程调试 ...

  10. SQL Sever 2008性能分析之执行计划

    一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试 2的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<Inside Microsoft SQL ...