1.Action Filter

Action Filter提供了在执行action/controller前后对请求/响应修改的能力,可以应用于action和控制器上,作用在控制器上将自动被应用到该控制器下所有的action.

常见的应用场景例如: 用户验证授权, 修改view的返回结果,修改response头,日志记录,异常/错误处理等 。

AspNet MVC包含四种类型的action filter:

a.Auth Filter 验证过滤器,实现IAuthorizationFilter,用于针对action或者controller访问时候的用户身份验证或者授权验证。例如mvc自带的[Authorze]

b.Action Filter 操作过滤器,实现IActionFilter接口,包含一些逻辑,用于Action执行前后前后被调用,可以用于处理修改类似request的参数或者response参数.

c.Result filter 结果过滤器,实现IResultFilter接口,包含一些逻辑,用于直线ViewResult执行前后被调用,常见的应用就是修改返回的view.

d.Exception filter 异常过滤,实现IExceptionFilter,用于action/controller中异常/错误的处理.

默认以上四种类型的filter执行顺序是:

『 Auth ---- Action ---- Result ---- Exception  』

身份验证永远最先被执行,异常处理最后被执行,如果想打破这种默认的执行的顺序,可以通过修改FilterAttribute的Order来实现,FilterAtribute的Order实现与接口IMvcFilter

public interface IMvcFilter{
//
int Order{get;}
}
public class FilterAttribute: Atrribute, IMvcFilter
{
//
public int Order{get; set;}
}

ps:想验证filter指定默认的执行顺序,可以分别实现对应的接口和FilterAttribute接口然后输出order属性看看,例如:

//继承FilterAttribute主要是为了能像属性一样应用fliter
public class AFilterAttribute: FilterAttribute, IActionFilter
{
public void OnActionExecuted(){}
public void OnActionExecuting(){ ==>output :order }
} public class MyController: Controller{
[AFilter]
public string Index(){/}
} 如果想实现全局应用所有请求或者不想这种声明是添加可以通过GlobalFilterCollection添加过滤器.
GlobalFilterCollection ::Add( your filter );

2.自定义action filter

自定义过滤器主要实现对应的过滤器接口就可以了,Auth过滤器,实现IAuthorizationFilterjie接口的OnAuthorization方法就可以了(或者直接继承AuthorieAttribute).Exception过滤器实现IExceptionFilter::OnException方法,如果要实现action/result可以继承ActionFilterAttribute类.

ActionFilterAttribue继承了FilterAttribute,实现了IActionFilter,IResultFilter接口.

FilterAttribute <-- { class: Attribute , Interface:IMvcFilter }

|

|

ActionFilterAttribute  <---{ interface: IActionFilter, IResultFilter }

|

|

custom filter

interface: IActionFilter {

Method: OnActionExecuting , OnActionExecuted

}

interace:IResultFilter{

Method: OnResultExecuting/OnResultExecuted

}

自定义过滤器可以实现 OnActionExecuting /OnActionExecuted 用于在action执行前后分别处理一些自定义逻辑,比如修改view data数据

重写OnResultExecuting/OnResultExecuted 用于在view redner前后执行一些自定义逻辑,比如修改呈现的view

AspNet MVC : 操作/控制器过滤器(action filter)的更多相关文章

  1. ASP.NET MVC 4.0的Action Filter

    有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你自定义创建action过滤器.Action过滤器是自定义的Attributes,用来 ...

  2. [转]ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)

    有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你创建action过滤器.Action过滤器是自定义的Attributes,用来标记添 ...

  3. ASP.NET MVC 入门9、Action Filter 与 内置的Filter实现(介绍)

    原帖地址:http://www.cnblogs.com/QLeelulu/archive/2008/10/09/1307660.html 有时候你想在调用action方法之前或者action方法之后处 ...

  4. ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)

    于ASP.NET MVC Preview5. 前一篇中我们已经了解了Action Filter 与 内置的Filter实现,现在我们就来写一个实例.就写一个防盗链的Filter吧. 首先继承自Filt ...

  5. [转]ASP.NET MVC 入门10、Action Filter 与 内置的Filter实现(实例-防盗链)

    前一篇中我们已经了解了Action Filter 与 内置的Filter实现,现在我们就来写一个实例.就写一个防盗链的Filter吧. 首先继承自FilterAttribute类同时实现IAction ...

  6. 返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test

    原文:返璞归真 asp.net mvc (5) - Action Filter, UpdateModel, ModelBinder, Ajax, Unit Test [索引页] [源码下载] 返璞归真 ...

  7. AspNet MVC与T4,我定制的视图模板

    一. 遇到的问题 文章开头部分想先说一下自己的困惑,在用AspNet MVC时,完成Action的编写,然后添加一个视图,这个时候弹出一个添加视图的选项窗口,如下: 很熟悉吧,继续上面说的,我添加一个 ...

  8. 使用ASP.NET MVC操作过滤器记录日志(转)

    使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...

  9. [翻译] 使用ASP.NET MVC操作过滤器记录日志

    [翻译] 使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_ ...

随机推荐

  1. zoj 3659 Conquer a New Region(并查集)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 代码: #include<cstdio> #inc ...

  2. zoj 3656

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4879 代码: #include<cstdio> #inc ...

  3. java对文件拷贝的简单操作

    package fileInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  4. java实现链表结构

    1. 定义节点node public class Node<T> { private Node<T> pre; private Node<T> next; priv ...

  5. ListView 分类: WinForm 2014-07-18 22:03 289人阅读 评论(0) 收藏

    一.ListView类(转载) 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLin ...

  6. 触发器记录表某一个字段数据变化的日志 包括插入insert 修改update 删除delete 操作

    本文参考:http://www.cnblogs.com/lyhabc/articles/3236985.html ,),  ),               ),           ),       ...

  7. Android开源项目分类汇总【畜生级别】[转]

    Android开源项目分类汇总 欢迎大家推荐好的Android开源项目,可直接Commit或在 收集&提交页 中告诉我,欢迎Star.Fork :) 微博:Trinea    主页:www.t ...

  8. SQL字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...

  9. [JS][jQuery]remove()与 empty()的差别

    要用到移除指定元素的时候,发现empty()与remove([expr])都能够用来实现.可细致观察效果的话就能够发现. empty()是仅仅移除了 指定元素中的全部子节点.拿$("p&qu ...

  10. 使用r2d3的注意事项

    避免使用tspan 样式上display:none无效, visibility:hidden无效,只能使用fill:none或rgba(0,0,0,0) 容器不能起到绘图的层级作用,覆盖顺序由先后顺序 ...