Filter,在MVC中我们通常将Filter定义成Attribute特性 来供Controller 或者Action 方法调用。 FilterAttribute 是所有Filter 的基类。

而 FilterAttribute 实现了IMvcFilter 接口。

GIobalFilterCollection :全局Filter。如下代码注册全局 Filter。

  public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
GlobalFilters.Filters.Add(new BazAttribute());
}

自定义Filter特性

    public abstract class FilterBaseAttribute : FilterAttribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{ } public void OnActionExecuting(ActionExecutingContext filterContext)
{ }
} public class FooAttribute : FilterBaseAttribute
{ }
public class BarAttribute : FilterBaseAttribute
{ }
public class BazAttribute : FilterBaseAttribute
{ }

AttributeUsageAttribute: 设置AllowMultiple=false,表示filter在同一个目标元素上只能使用一次 如下代码

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class FooAttribute : FilterAttribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{ }
public void OnActionExecuting(ActionExecutingContext filterContext)

然后我们在三个地方设置 Foo的Filter

全局:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//filters.Add(new HandleErrorAttribute());
GlobalFilters.Filters.Add(new FooAttribute());
}

controller 和 action

    [Foo]
public class HomeController : Controller
{
public ActionResult Index()
{
ReflectedControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(typeof(HomeController));
ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(ControllerContext, "DemoAction");
IEnumerable<Filter> filters = FilterProviders.Providers.GetFilters(ControllerContext, actionDescriptor);
return View(filters);
} [Foo]
public void DemoAction()
{ }
}

运行会发现 只有一个filter被执行了。

FiterInfo:

MVC4 Filter (筛选器)的更多相关文章

  1. Filter 筛选器(一)之 ActionFilter-- IAsyncActionFilter 和 ActionFilterAttribute

    微软官网例子:Filter筛选器 使用场景(执行顺序): IAsyncActionFilter 使用异步actionFilter 只需要实现 他的 :OnActionExecutionAsync 方法 ...

  2. Filter 筛选器(三)之 自定义一个启动事务的 TransactionScopeFilter

    如果一个方法内有多个写入操作,比如 写入A表,然后用A表的自增id 去写入B表,假如A表写入成功,但B表因为某种原因写入失败!(这就导致A表写入了脏数据) 这时候 我们可以自定义 一个Filter 进 ...

  3. Filter 筛选器(二)之 ExceptionFilter

    public class MyExceptionFilter : IAsyncExceptionFilter { private readonly ILogger<MyExceptionFilt ...

  4. MVC四大筛选器—ActionFilter&ResultedFilter

    AuthorizeFilter筛选器 在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向 ...

  5. MVC四大筛选器—AuthorizeFilter

    在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向方面编程)的方式来设计的,通过对Act ...

  6. 表示层设计模式:Intercepting Filter(截取筛选器)模式

     上下文  问题  影响因素  解决方案  变体  示例  结果上下文  相关模式  致谢  上下文 对于任何一个曾经从头建立 Web 应用程序的人来说,他们都会有这样的体会:这项任务所需要的独立完成 ...

  7. Filter List Views 筛选器列表视图

    In this lesson, you will learn how to filter a List View. Three techniques, based on different scena ...

  8. Asp.Net MVC 页面代码压缩筛选器-自定义删除无效内容

    Asp.Net MVC 页面代码压缩筛选器 首先定义以下筛选器,用于代码压缩. /*页面压缩 筛选器*/ public class WhiteSpaceFilter : Stream { privat ...

  9. WEB服务器5--IIS中ISAPI扩展、ISAPI筛选器

    在IIS的文档中经常会提到两个术语:ISAPI扩展和ISAPI筛选器. ISAPI扩展 “ISAPI扩展(ISAPI Extension)”是一种可以添加到IIS中以增强Web服务器功能的程序,其载体 ...

随机推荐

  1. HTTP与TCP/IP的区别

    TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.关于TCP/IP和HTTP协议的关系,网络有一段比较容易理解的介绍:“我们在传输数据时,可以只 ...

  2. PHP编译安装系列

    徐亮伟, 江湖人称标杆徐.多年互联网运维工作经验,曾负责过大规模集群架构自动化运维管理工作.擅长Web集群架构与自动化运维,曾负责国内某大型电商运维工作. 个人博客"徐亮伟架构师之路&quo ...

  3. leetcode36

    public class Solution { public bool IsValidSudoku(char[,] board) { ; i < ; i++) { var dic = new D ...

  4. c++可变参数(示例)

    #include "stdafx.h" #include <stdarg.h> // 必须包含的头文件 #define ADD(int_params,...) Add( ...

  5. python 数值计算库

    pip install numpy pip install matplotlib pip install sklearn yum -y install tkinter pip install scip ...

  6. cdoj1091-秋实大哥の恋爱物语 【kmp】

    http://acm.uestc.edu.cn/#/problem/show/1091 秋实大哥の恋爱物语 Time Limit: 5000/2000MS (Java/Others)     Memo ...

  7. cdoj913-握手 【Havel定理】

    http://acm.uestc.edu.cn/#/problem/show/913 握手 Time Limit: 2000/1000MS (Java/Others)     Memory Limit ...

  8. 【BZOJ 3261】最大异或和【可持久化字典树】

    题意 给出一个长度为n的整数序列,给出m个操作.操作有两种.1,Ax表示在序列结尾增加x.2,Qlrx表示找到一个位置p满足 l<=p<=r,使得a[p] xor a[p+1]xor... ...

  9. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  10. linux 安装天气插件

    1. 用命令 sudo apt install gnome-shell-extension-weather 安装插件 但是在那之前你安装了 GNOME Shell Extensions 如果没有安装 ...