ActionInvoker In ASP.NET MVC

In this article we will be discussing how to invoke the action methods base of custom logic and how to redirect a request for an action method to another action method in the same controller or another controller.

We can achieve this with the below two different ways,

  • Controller Action Invoker
  • Action Invoker

Now  let’s discuss them briefly.

Controller Action Invoker

This invokes the specified action by using specific controller context. By implementing ControllerActionInvoker class and overriding InvokeAction action method you can achieve the same. InvokeAction is having controller context and action name as parameter and it returns the result of the executing action.

Let’s see this with the below example.

  • Add folder Utilities
  • Now add class CustomControllerInvoker as shown below
  1. public class CustomControllerInvoker:ControllerActionInvoker  
  2.     {  
  3.         public override bool InvokeAction(ControllerContext controllerContext, string actionName)  
  4.         {  
  5.             if (actionName.Equals("Index"))  
  6.             {  
  7.                 ViewResult result = new ViewResult();  
  8.                 result.View = result.ViewEngineCollection.FindView(controllerContext, "About"null).View;  
  9.                 InvokeActionResult(controllerContext, result);  
  10.                 return true;  
  11.             }  
  12.             else  
  13.             {  
  14.                 return base.InvokeAction(controllerContext, actionName);  
  15.             }  
  16.   
  17.               
  18.         }  
  19.     }  

Now, above, we have inherited ControllerActionInvoker in CustomControllerInvoker and have overridden InvokeAction. InvokeAction will check if incoming request is for Index action method then it will redirect it to About action method else it will execute called action method.

Now assign CustomControllerInvoker, the invoker of Home controller.
  1. public  HomeController()  
  2.  {  
  3.       this.ActionInvoker = new CustomControllerInvoker();  
  4.        
  5.  }  
Action Invoker

This invokes the specified action by using specific controller context. By implementing IActionInvoker interface and overriding InvokeAction action method you can achieve the same. InvokeAction has controller context and action name as parameter and it returns result of executing action.

Let’s see this with the below example,

Now add class CustomActionInvoker in Utilities folder as shown below.
  1. public class CustomActionInvoker : IActionInvoker  
  2.     {  
  3.         public bool InvokeAction(ControllerContext controllerContext, string actionName)  
  4.         {  
  5.             if (actionName.Equals("About", StringComparison.CurrentCultureIgnoreCase))  
  6.             {  
  7.                 controllerContext.HttpContext.Response.Write("This should be the home page");  
  8.                 return true;  
  9.             }  
  10.             else  
  11.             {  
  12.                 return false;  
  13.             }  
  14.         }  
  15.     }  

Now, above, we have inherited IActionInvoker in CustomActionInvoker and have overridden InvokeAction. InvokeAction will check if incoming request is for About action method then it will show the message “This should be the home page."

Now assign CustomActionInvoker the invoker of Home controller, as shown below.
  1. public  HomeController()  
  2.  {  
  3.       this.ActionInvoker = new CustomActionInvoker  
  4.  }  


Similar Articles