\t\tASP.NET MVC ActionFilterAttribute

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

一些可能用到Action过滤器的地方有:

  • 日志
  • 身份验证和授权 - 限制用户的访问
  • 输出缓存 - 保存一个Action的结果
  • 网络爬虫的过滤
  • 本地化
  • 动态Action - 将一个Action注入到控制器中

实现一个Action过滤器

Action过滤器是通过继承ActionFilterAttribute类来实现的一个Attribute类。ActionFilterAttribute 是一个抽象类,提供了两个virtual的方法给我们重写,OnActionExecutingOnActionExecuted

ASP.NET MVC 框架会在调用Action方法之前调用你Action过滤器中的OnActionExecuting方法,在之后调用Action过滤器中的OnActionExecuted方法。当然在创建Action过滤器的时候你不需两个方法都实现。

下面的示例是在调用Action方法之前和之后的日志跟踪:

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客 		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客 LoggingFilterAttribute
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客public class LoggingFilterAttribute : ActionFilterAttribute
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public override void OnActionExecuting(FilterExecutingContext
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext.HttpContext.Trace.Write(
"Starting: " +
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext.ActionMethod.Name);
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public override void OnActionExecuted(FilterExecutedContext
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客       
if (filterContext.Exception != null)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客       
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客            filterContext.HttpContext.Trace.Write(
"Exception thrown");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客}

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方法。

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客 public class HomeController : Controller
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    [LoggingFilter]
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void Index()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        RenderView(
"Index");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    [LoggingFilter]
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void About()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        RenderView(
"About");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    [LoggingFilter]
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void ClickMe()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        HttpContext.Trace.Write(
"Button was clicked.");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        InvokeAction(
"Index");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客}

实现一个控制器范围的Action过滤器

ASP.NET MVC 控制器(Controller)类定义的OnActionExecuting 和 OnActionExcuted 方法你可以重写。当你重写一个或者这两个方法的时候,你实际上定义了一个将会应用到该控制器类中所有的Action方法的Action过滤器。严格来说,这个方法没有构成一个Action过滤器,但不管怎样,她们提供的功能是相似的。
在下面的示例中,控制器级别的OnActionExecuting和OnActionExecuted方法应用到控制器中所有的Action方法中:

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客 		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客 控制器范围的Action Filter
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客public class HomeController : Controller
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void Index()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        RenderView(
"Index");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void About()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        RenderView(
"About");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
public void ClickMe()
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        HttpContext.Trace.Write(
"Button was clicked.");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        InvokeAction(
"Index");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
protected override void OnActionExecuting(FilterExecutingContext
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext.HttpContext.Trace.Write(
"Starting: " +
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客            filterContext.ActionMethod.Name);
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
protected override void OnActionExecuted(FilterExecutedContext
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        filterContext)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客   
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客       
if (filterContext.Exception != null)
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客       
{
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客            filterContext.HttpContext.Trace.Write(
"Exception thrown");
		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客        }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客    }

		ASP.NET MVC ActionFilterAttribute - yandavid - 我的博客}


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属性值,将会抛出一个异常。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值