简体   繁体   English

实现FilterAttribute,IActionFilter和从asp.net mvc 3中的ActionFilterAttribute继承有什么区别?

[英]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类的OnActionExecutingOnActionExecuted方法,如下所示:

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: 在其他情况下,我们也可以像这样实现IActionFilterFilterAttribute

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.

相关问题 ASP.NET MVC中<%:和<%=有什么区别? - What is the difference between <%: and <%= in ASP.NET MVC? ASP.NET 和 ASP.NET MVC 有什么区别? - What is the difference between ASP.NET and ASP.NET MVC? ASP.NET MVC3 + ActionFilterAttribute +注入? - ASP.NET MVC3 + ActionFilterAttribute + Injection? ASP.NET MVC 6 中 FilterAttribute 内的依赖注入 - Dependency injection inside a FilterAttribute in ASP.NET MVC 6 Asp.Net MVC 4-ActionFilterAttribute用法 - Asp.Net MVC 4 - ActionFilterAttribute Usage ASP.NET MVC 5、ASP.NET Core MVC 5 有什么区别? - What is the difference between ASP.NET MVC 5, ASP.NET Core MVC 5? ASP.NET MVC中ViewModel和Model之间的主要区别是什么? - What's the key difference between a ViewModel and a Model in ASP.NET MVC? ASP.NET MVC中的ResourceControllerFactory和DefaultControllerFactory有什么区别? - What is the difference between the ResourceControllerFactory and the DefaultControllerFactory in ASP.NET MVC? Asp.net 页面生命周期和 Asp.net Mvc 页面生命周期有什么区别? - what is the difference between Asp.net page life cycle and Asp.net Mvc page life cycle? ASP.NET中的“ ID”和“ BehaviorID”属性有什么区别? - What's the difference between the property “ID” and “BehaviorID” in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM