2009-05-28 20 views

答えて

4

Professional ASP.NET MVC 1.0の本によると、コントローラがインスタンス化された後にActionFiltersが実行されます。 OnActionExecuting(ActionFilterが呼び出す最初のメソッド)の時点で、Controllerコンテキストが利用可能です。

2

コントローラは、アクションフィルタのOnActionExecutedイベントとOnActionExecutingイベントが発生する前にインスタンス化されます。また、イベントハンドラに渡される "filterContext"パラメータを介してコントローラにアクセスすることもできます。

public class TestActionAttribute : FilterAttribute, IActionFilter 
{ 
    #region IActionFilter Members 

    public void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var controller = filterContext.Controller; 
    } 

    public void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var controller = filterContext.Controller; 
    } 

    #endregion 
} 
1

抽象クラスSystem.Web.Mvc.ActionFilterAttribute(このクラスから独自のActionFilterを導き出す)4つのXXXのメソッドを持っています:

  • OnActionExecuting
  • OnResultExecuting
  • をOnActionExecuted
  • をOnResultExecuted

私はOnActionExecutingであなたのコントローラをチェックすることができると思う:

YourController controller = filterContext.Controller as YourController 
if(controller != null) 
{ 
    // check your controller 
} 
関連する問題