[英]What's the difference between implementing FilterAttribute, IActionFilter and inheriting from ActionFilterAttribute in asp.net mvc 3?
I see that in one situation we can override OnActionExecuting
or OnActionExecuted
methods inheriting from ActionFilterAttribute
class like this: 我看到在一种情况下我们可以覆盖继承自ActionFilterAttribute
类的OnActionExecuting
或OnActionExecuted
方法,如下所示:
public class MyFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuted(ActionExecutedContext filterContext)
{ // bla bla }
}
And in other situation we also can implement IActionFilter
and FilterAttribute
like this: 在其他情况下,我们也可以像这样实现IActionFilter
和FilterAttribute
:
public class MySecondFilterAttribute : FilterAttribute, IActionFilter
{
public void OnActionExecuted(ActionExecutingContext filterContext) {}
}
So, is there any differences between these two approaches, maybe any particular situations where it would be preferable to use one of them over the other?? 那么,这两种方法之间是否有任何差异,可能是任何特殊情况下,最好使用其中一种方法而不是另一种方法?
Thanks in advance. 提前致谢。
Basically FilterAttribute provides the most low level behaviour of MVC Attributes and implements the IMvcFilter that provides the Order and AllowMultiple properties. 基本上,FilterAttribute提供MVC属性的最低级别行为,并实现提供Order和AllowMultiple属性的IMvcFilter。
ActionFilterAttribute is the basis for filtering actions and results, since is a implementation of IActionFilter, IResultFilter and inherit from FilterAttribute. ActionFilterAttribute是过滤操作和结果的基础,因为它是IActionFilter,IResultFilter的实现,并从FilterAttribute继承。
Your MySecondFilterAttribute implementation leads to ActionFilterAttribute without IResultFilter capabilities (OnResultExecuting and OnResultExecuted). 您的MySecondFilterAttribute实现导致ActionFilterAttribute没有IResultFilter功能(OnResultExecuting和OnResultExecuted)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.