3

私はかなり大きな MVC Web アプリケーションを持っており、各コントローラーでアクションを繰り返して、顧客がアプリケーションプロセスを完了したかどうかを確認しています (この場合、チェックするプロファイルにフラグが設定されています)。理想的には、各アクション メソッドからこのコードを削除し、アクション結果を返すすべてのアクション メソッドに適用したいと考えています。

4

2 に答える 2

3

それを使用するよりも、予想されるすべてのコントローラーが何らかの BaseController から継承された場合、共通の動作を設定できます。

public class HomeController : BaseController
{
}

BaseContoller は次のようになります

public class BaseController : Controller
{
     protected BaseController(common DI)
     {
     }

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
      // some logic after action method get executed      
        base.OnActionExecuted(filterContext);
    }

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
       // some login before any action method get executed

       string actionName = filterContext.RouteData.Values["action"].ToString(); // Index.en-US

        filterContext.Controller.ViewBag.SomeFlage= true;
    }

  // If Project is in MVC 4 - AsyncContoller support, 
  //below method is called before any action method get called, Action Invoker

   protected override IActionInvoker CreateActionInvoker()
    {       
        return base.CreateActionInvoker();
    }
}
于 2013-09-24T08:07:26.867 に答える