MVC4 Filter (筛选器)
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 (筛选器)的更多相关文章
- Filter 筛选器(一)之 ActionFilter-- IAsyncActionFilter 和 ActionFilterAttribute
微软官网例子:Filter筛选器 使用场景(执行顺序): IAsyncActionFilter 使用异步actionFilter 只需要实现 他的 :OnActionExecutionAsync 方法 ...
- Filter 筛选器(三)之 自定义一个启动事务的 TransactionScopeFilter
如果一个方法内有多个写入操作,比如 写入A表,然后用A表的自增id 去写入B表,假如A表写入成功,但B表因为某种原因写入失败!(这就导致A表写入了脏数据) 这时候 我们可以自定义 一个Filter 进 ...
- Filter 筛选器(二)之 ExceptionFilter
public class MyExceptionFilter : IAsyncExceptionFilter { private readonly ILogger<MyExceptionFilt ...
- MVC四大筛选器—ActionFilter&ResultedFilter
AuthorizeFilter筛选器 在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向 ...
- MVC四大筛选器—AuthorizeFilter
在Action的执行中包括两个重要的部分,一个是Action方法本身逻辑代码的执行,第二个就是Action方法的筛选器的执行. MVC4中筛选器都是以AOP(面向方面编程)的方式来设计的,通过对Act ...
- 表示层设计模式:Intercepting Filter(截取筛选器)模式
上下文 问题 影响因素 解决方案 变体 示例 结果上下文 相关模式 致谢 上下文 对于任何一个曾经从头建立 Web 应用程序的人来说,他们都会有这样的体会:这项任务所需要的独立完成 ...
- Filter List Views 筛选器列表视图
In this lesson, you will learn how to filter a List View. Three techniques, based on different scena ...
- Asp.Net MVC 页面代码压缩筛选器-自定义删除无效内容
Asp.Net MVC 页面代码压缩筛选器 首先定义以下筛选器,用于代码压缩. /*页面压缩 筛选器*/ public class WhiteSpaceFilter : Stream { privat ...
- WEB服务器5--IIS中ISAPI扩展、ISAPI筛选器
在IIS的文档中经常会提到两个术语:ISAPI扩展和ISAPI筛选器. ISAPI扩展 “ISAPI扩展(ISAPI Extension)”是一种可以添加到IIS中以增强Web服务器功能的程序,其载体 ...
随机推荐
- CentOS 修改源为163和指定epel源和docker安装
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- Android Studio 无法预览布局问题:com/android/util/PropertiesMap
应该是API版本太高,换成较低的就好了 API24,无法预览 换成22就没事了 Android Studio要比Eclipse好用很多,虽然Eclipse现在可以直接安装Android开发板,但AS界 ...
- leetcode477
public class Solution { public int TotalHammingDistance(int[] nums) { , n = nums.Length; ; j < ; ...
- 「小程序JAVA实战」小程序视图之条件判断(15)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-15/ 小程序里面也是有条件判断的,我相信大家在开发java if和jstl c:if c:when ...
- 分环境部署SpringBoot日志logback-spring.xml
springboot按照profile进行打印日志 log4j logback slf4j区别? 首先谈到日志,我们可能听过log4j logback slf4j这三个名词,那么它们之间的关系是怎么样 ...
- Rust 初始配置
学习 Rust 初始配置 运行环境:Window7 64bit,Rust nightly 1.23; 作者:乌龙哈里 2017-10-15 参考: Rust 中文教程 Rust 官方网站 Rust G ...
- MonoDevelop Assembly Browser
[MonoDevelop Assembly Browser] View -> Assembly Browser,通过此窗口可以查看Dll的反编译后的代码. 还有几款免费的替代产品可以使用, 虽然 ...
- ShadowVolume
[ShadowVolume] 1.z-pass 算法. z-pass 是 shadow volume 一开始的标准算法,用来确定某一个象素是否处于阴影当中.其原理是: Pass1:enable z-b ...
- Blending
[Blending] Blending is used to make transparent objects. When graphics are rendered, after all shade ...
- easyui图标大全
.icon-blank{ background:url('icons/blank.gif') no-repeat; } .icon-add{ background:url('icons/edit_ad ...