37

ある状況では、次のようにクラスから継承するメソッドOnActionExecutingをオーバーライドできることがわかります。OnActionExecutedActionFilterAttribute

public class MyFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    { // bla bla }
}

そして他の状況では、私たちはこれを実装IActionFilterしてFilterAttribute好きにすることもできます:

public class MySecondFilterAttribute : FilterAttribute, IActionFilter 
{ 
    public void OnActionExecuted(ActionExecutingContext filterContext) {}
}

それで、これらの2つのアプローチの間に違いはありますか、おそらく一方を他方よりも使用することが望ましい特定の状況はありますか?

前もって感謝します。

4

1 に答える 1

28

基本的に、FilterAttribute は MVC 属性の最も低レベルの動作を提供し、Order プロパティと AllowMultiple プロパティを提供する IMvcFilter を実装します。

ActionFilterAttribute は、IActionFilter、IResultFilter の実装であり、FilterAttribute から継承するため、アクションと結果をフィルタリングするための基礎です。

MySecondFilterAttribute 実装は、IResultFilter 機能 (OnResultExecuting および OnResultExecuted) のない ActionFilterAttribute につながります。

于 2013-02-14T22:32:53.650 に答える