ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html
ASP.NET MVC : Action过滤器(Filtering)
相关文章:
有时候你想在调用action方法之前或者action方法之后处理一些逻辑,为了支持这个,ASP.NET MVC允许你创建action过滤器。Action过滤器是自定义的Attributes,用来标记添加Action方法之前或者Action方法之后的 行为到控制器类中的Action方法中。
一些可能用到Action过滤器的地方有:
- 日志
- 身份验证和授权 - 限制用户的访问
- 输出缓存 - 保存一个Action的结果
- 网络爬虫的过滤
- 本地化
- 动态Action - 将一个Action注入到控制器中
实现一个Action过滤器
Action过滤器是通过继承ActionFilterAttribute类来实现的一个Attribute类。ActionFilterAttribute 是一个抽象类,提供了两个virtual的方法给我们重写,OnActionExecuting和OnActionExecuted。
ASP.NET MVC 框架会在调用Action方法之前调用你Action过滤器中的OnActionExecuting方法,在之后调用Action过滤器中的OnActionExecuted方法。当然在创建Action过滤器的时候你不需两个方法都实现。
下面的示例是在调用Action方法之前和之后的日志跟踪:
{
public override void OnActionExecuting(FilterExecutingContext
filterContext)
{
filterContext.HttpContext.Trace.Write("Starting: " +
filterContext.ActionMethod.Name);
} 
public override void OnActionExecuted(FilterExecutedContext
filterContext)
{
if (filterContext.Exception != null)
{
filterContext.HttpContext.Trace.Write("Exception thrown");
}
}
}Action Filter Context
OnActionExecuting方法有一个类型为FilterExecutingContext的参数,而OnActionExecuted方法有一个相应的类型为FilterExcutedContext的
参数。两个Context类都是继承自FilterContext类,而FilterContext类继承自ControllerContext类并包含
一个ActionMethod属性。你可以使用ActionMethod属性来坚定这个Action过滤器是应用到哪个Action方法上的。
FilterExecutingContext类包含一个 Cancel 的属性,允许你取消当前的Action。
FilterExcutedContext
类包含一个Exception属性和一个ExceptionHandled属性。如果Exception属性为null,则没有异常在action
stack中,表明Action方法运行并没有发生错误。如果Exception属性不为null,则过滤器知道该怎么处理,过滤器处理完异常后会发出已
经处理完的信号,然后将ExceptionHandled属性设为true。就算ExceptionHandled属性为true,堆栈中添加到其他
Action方法的OnActionExcetued方法将会照常被调用,这种场景就如就算一个异常被处理了,日志记录filter一样照常执行。
用过滤器特性(Attribute)来标记一个Action方法
你可以将过滤器应用到任何一个你喜欢的Action方法上。下面的示例演示一个控制器中包含的用Action过滤器Attribute标记的Action方法。
{
[LoggingFilter]
public void Index()
{
RenderView("Index");
} 
[LoggingFilter]
public void About()
{
RenderView("About");
} 
[LoggingFilter]
public void ClickMe()
{
HttpContext.Trace.Write("Button was clicked.");
InvokeAction("Index");
}
}实现一个控制器范围的Action过滤器
ASP.NET MVC 控制器(Controller)类定义的OnActionExecuting 和
OnActionExcuted
方法你可以重写。当你重写一个或者这两个方法的时候,你实际上定义了一个将会应用到该控制器类中所有的Action方法的Action过滤器。严格来说,
这个方法没有构成一个Action过滤器,但不管怎样,她们提供的功能是相似的。
在下面的示例中,控制器级别的OnActionExecuting和OnActionExecuted方法应用到控制器中所有的Action方法中:
Action过滤器的作用范围
除了用Action过滤器标记一个Action方法外,你也可以用来标记一个完成的控制器类。如果这样的话,这个Action过滤器将会应用到该控制器的所有Action方法上。
另外,如果你的控制器类继承自别的控制器类,而基控制器类可能有它自己的Action过滤器Attributes。如果你在子类中重写了基控制器类的Action方法,则子类的该Action方法也会有它自己的从基类继承而来的Action过滤器Attributes。
Action过滤器的执行顺序
每一个Action过滤器都有一个 Order
属性,用来决定Action过滤器在该范围内的执行顺序。Order属性必需是0(默认值)或者更大的整数值。省略Order属性则会给该过滤器的
Order值为 -1, 表明为指明顺序。任何一个在同一范围的Action过滤器Order设为 -1
的都将按不确定的顺序执行,单在此之前过滤器有一个特定的顺序(注:下面会说到).
当设置Order属性的值的时候,必需指定一个唯一的值。如果两个或者更多的Action过滤器具有相同的Order属性值,将会抛出一个异常。
来看一个示例:
[Filter2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
RenderView("Index");
}
Filter的执行顺序为:Filter3 => Filter1 => Filter2.
在同一范围,Action过滤器按下面的顺序执行:
- OnActionExecuting virtual method of the controller.
- OnActionExecuting method of any filters that are applied to the current controller, in this order:
Base class
Any derived class
- OnActionExecuting method of filters that are applied to the action method, in this order:
Base class
Derived class
- Action method
- OnActionExecuted method of filters that are applied to the action method, in this order:
Derived class
Base class
- OnActionExecuted method of filters that are applied to the current controller, in this order:
Derived class
Base class
- OnActionExecuted virtual method of the controller.
可以看到OnActionExecuted 和 OnActionExecuting 的执行顺序是反过来的。
Action过滤器执行顺序的示例
下面的示例演示了一个包含两个Action过滤器的MVC程序。DebugFilter 过滤器写trace信息,而ThrowExceptionFilter 过滤器引起一个异常。这个程序也包含一个基控制器和一个子控制器和一个视图。
下面的Action过滤器在调用的时候将会抛出一个异常:
{
public override void
OnActionExecuting(
System.Web.Mvc.FilterExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
DebugFilterAttribute.StartDecrement();
throw new InvalidOperationException(
"Exception thrown by the filter");
}
}下面的是定义一个应用了Action过滤器的基控制器类:
[DebugFilter(Message = "(CONTROLLER) MyBaseController", Order=2)]
public class MyBaseController : Controller
{
[ControllerAction]
[DebugFilter(Message = "(ACTION) MyBaseController.Index()", Order=2)]
[DebugFilter(Message = "(ACTION) MyBaseController.Index()", Order=1)]
public virtual void Index()
{
RenderView("Index");
}
}下面的是子控制器类,注意这里重写了OnActionExecuting 方法:
下面的是显示trace的视图页:
下面是程序运行时的输出结果:
Action Filter 2: (PRE) DebugFilter.OnActionExecuting - Order=1 Message='(CONTROLLER) MyBaseController
Action Filter 3: (PRE) DebugFilter.OnActionExecuting - Order=2 Message='(CONTROLLER) MyBaseController
Action Filter 4: (PRE) DebugFilter.OnActionExecuting - Order=1 Message='(CONTROLLER) MyDerivedController
Action Filter 5: (PRE) DebugFilter.OnActionExecuting - Order=2 Message='(CONTROLLER) MyDerivedController
Action Filter 6: (PRE) DebugFilter.OnActionExecuting - Order=1 Message='(ACTION) MyDerivedController.Index()
Action Filter 7: (PRE) DebugFilter.OnActionExecuting - Order=2 Message='(ACTION) MyDerivedController.Index()
Action Filter 8: (ACTION) MyDerivedController.Index()
ASP.NET MVC : Action过滤器(Filtering)的更多相关文章
- Asp.Net Mvc Action过滤器(二)
在Mvc中为Action添加过滤器,有两种方式, 一.使用ActionFilterAttribute,简单方式,同时支持Result的过滤处理, 1.可以为空,支持的重写:OnActionExecut ...
- 使用ASP.NET MVC操作过滤器记录日志(转)
使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...
- [翻译] 使用ASP.NET MVC操作过滤器记录日志
[翻译] 使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_ ...
- ASP.NET MVC动作过滤器
ASP.NET MVC提供了4种不同的动作过滤器(Aciton Filter). 1.Authorization Filter 在执行任何Filter或Action之前被执行,用于身份验证 2.Act ...
- Asp.net MVC 之过滤器
整理一下MVC中的几种过滤器,以及每种过滤器是干什么用的 四种过滤器 1.AuthorizationFilter(授权过滤器) 2.ActionFilter(方法过滤器) 3.ResultFilter ...
- Asp.net MVC 权限过滤器实现方法的最佳实践
在项目开发中,为了安全.方便地判断用户是否有访问当前资源(Action)的权限,我们一般通过全局过滤器来实现. Asp.net MVC 页面中常见的权限判断使用过滤器主要在以下几种情况(根据权限判断的 ...
- ASP.NET MVC 系统过滤器、自定义过滤器
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- Asp.Net MVC在过滤器中使用模型绑定
废话不多话,直接上代码 1.创建MVC项目,新建一个过滤器类以及使用到的实体类: public class DemoFiltersAttribute : AuthorizeAttribute { pu ...
- ASP.NET MVC Action返回结果类型【转】
ASP.NET MVC 目前一共提供了以下几种Action返回结果类型: 1.ActionResult(base) 2.ContentResult 3.EmptyResult 4.HttpUnauth ...
随机推荐
- (转)linux grep 正则表达式
转自:http://www.cnblogs.com/xiaouisme/archive/2012/11/09/2762543.html -------------------------------- ...
- Windows2012安装IIS和FTP
Windows2012安装IIS和FTP 1.打开Windows 2012的服务器管理器,选择 管理→添加角色和功能 2.进入 添加角色和功能向导,选择 “Web服务器(IIS)” 3.根据情况选 ...
- git: windows git ssh keys生成
http://blog.csdn.net/lsyz0021/article/details/52064829 当我们使用github或者bitbucket等仓库时我们有可能需要ssh认证,所以需要生成 ...
- django template
一.模板基本元素 1.例子程序 1)urls.py中新增部分 from django.conf.urls import patterns, url, include urlpatterns = pat ...
- js高级程序设计(五)引用类型
Object类型 创建Object 实例的方式有两种.第一种是使用new 操作符后跟Object 构造函数. var person = new Object(); person.name = &quo ...
- SPSS数据分析—重复测量差分析
多因素方差分析中,每个被试者仅接受一种实验处理,通过随机分配的方式抵消个体间差异所带来的误差,但是这种误差并没有被排除.而重复测量设计则是让每个被试接受所有的实验处理,这样我们就可以分离出个体差异所带 ...
- WCF服务在类库中的引用
在类库中引用了WCF服务,悲剧降临了,追踪日志看到下边一串: --------------------------------------------------------------------- ...
- 三星在GPL下发布其exFAT文件系统实现源码
exFAT文件系统是微软的一个产品,设计让外置储存设备和PC之间实现无缝的TB级数据转移和数据交换,它只支持Windows和OS X,不支持Linux.作为一个含有大量专利的私有产品,没有人会预计它会 ...
- javaee包含的服务和组件
参考自 http://blog.itpub.net/29990276/viewspace-1318551/
- 黑客长期摇号不中"黑"掉北京小客车摇号网
新闻链接:http://www.2cto.com/News/201310/248936.html 新闻时间:2013-10-11 新闻正文: 为发泄长期摇号不中的不满,同时也为自己研发的软件打广告,硕 ...