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. Linux6系统安装

  2. Dbvisualizer设置SQL语句自动提示

    Dbvisualizer默认不自动提示SQL语句的命令及查询的表,虽然可以通过Ctrl+/快捷键进行手动调用出提示信息,用习惯了PLSQL Developer难免有些不适应. 设置自动提示方法: 点击 ...

  3. 关于Excel无法打开,因为文件格式或文件扩展名无效的解决方法

    可能有网友也遇到过跟我一样的情况,新建的excel一打开就弹出 这样的错误. 这是因为excel打开的时候会去指定的文件夹找一个模板,以这个模板的样式打开新建的ecxel.所以如果excel打开的时候 ...

  4. 遍历List集合时,删除数据的问题

    一.问题描述 有时候,我们会遇到在遍历List集合的过程中删除数据的情况. 看着自己写的代码,感觉完全没有问题,但就是达不到预期的效果,这是为什么呢?下面我们来分析下 String str1 = ne ...

  5. python gun readline

    https://github.com/ludwigschwardt/python-gnureadline

  6. S初始化生产环境数据

    一.将开发机的库文件导出10.10.1.139开发机服务器,桌面上的BAT文件,将数据库表结构和表数据导出来,导到E:\Repository,设置SADMIN密码永不过期BAT文件内容如下: ::导出 ...

  7. linux shell脚本编程笔记(二): 分支结构

    1.if if command then commands fi if command then commands else commands fi if command1 then command ...

  8. c++ 迭代器模式(iterator)

    提供一种方法顺序访问一个聚合对象中各个元素,而又不暴露该对象的内部表示.当你需要访问一个聚集对象,而且不管这些对象是什么都需要遍 历的时候,就应该考虑用迭代器模式.同时需要对聚集有多种方式遍历时,可以 ...

  9. svn配置多仓库与权限控制

    telnet: connect to address 47.106.115.228: Connection refused svn执行上下文错误由于目标计算机积极拒绝无法连接 标签: svn 2017 ...

  10. 基于Mybatis分页插件PageHelper

    基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...