How to call another controller Action From a controller in Mvc
C#asp.net Mvcasp.net Mvc-4ControllerC# Problem Overview
I need to call a controller B action FileUploadMsgView from Controller A and need to pass a parameter for it.
Its not going to the controller B's FileUploadMsgView()
.
Here's the code:
ControllerA:
private void Test()
{
try
{ //some codes here
ViewBag.FileUploadMsg = "File uploaded successfully.";
ViewBag.FileUploadFlag = "2";
RedirectToAction("B", "FileUploadMsgView", new { FileUploadMsg = "File uploaded successfully" });
}
}
ControllerB (receiving part):
public ActionResult FileUploadMsgView(string FileUploadMsg)
{
return View();
}
C# Solutions
Solution 1 - C#
As @mxmissile says in the comments to the accepted answer, you shouldn't new up the controller because it will be missing dependencies set up for IoC and won't have the HttpContext
.
Instead, you should get an instance of your controller like this:
var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
Solution 2 - C#
Controllers are just classes - new one up and call the action method just like you would any other class member:
var result = new ControllerB().FileUploadMsgView("some string");
Solution 3 - C#
Your sample looks like psuedo code. You need to return the result of RedirectToAction
:
return RedirectToAction("B",
"FileUploadMsgView",
new { FileUploadMsg = "File uploaded successfully" });
Solution 4 - C#
as @DLeh says Use rather
var controller = DependencyResolver.Current.GetService<ControllerB>();
But, giving the controller, a controlller context is important especially when you need to access the User
object, Server
object, or the HttpContext
inside the 'child' controller.
I have added a line of code:
controller.ControllerContext = new ControllerContext(Request.RequestContext, controller);
or else you could have used System.Web to acces the current context too, to access Server
or the early metioned objects
NB: i am targetting the framework version 4.6 (Mvc5)
Solution 5 - C#
Let the resolver automatically do that.
Inside A controller:
public class AController : ApiController
{
private readonly BController _bController;
public AController(
BController bController)
{
_bController = bController;
}
public httpMethod{
var result = _bController.OtherMethodBController(parameters);
....
}
}
Solution 6 - C#
If anyone is looking at how to do this in .net core I accomplished it by adding the controller in startup
services.AddTransient<MyControllerIwantToInject>();
Then Injecting it into the other controller
public class controllerBeingInjectedInto : ControllerBase
{
private readonly MyControllerIwantToInject _myControllerIwantToInject
public controllerBeingInjectedInto(MyControllerIwantToInject myControllerIwantToInject)
{
_myControllerIwantToInject = myControllerIwantToInject;
}
Then just call it like so _myControllerIwantToInject.MyMethodINeed();
Solution 7 - C#
This is exactly what I was looking for after finding that RedirectToAction()
would not pass complex class objects.
As an example, I want to call the IndexComparison
method in the LifeCycleEffectsResults
controller and pass it a complex class object named model.
Here is the code that failed:
return RedirectToAction("IndexComparison", "LifeCycleEffectsResults", model);
Worth noting is that Strings, integers, etc were surviving the trip to this controller method, but generic list objects were suffering from what was reminiscent of C memory leaks.
As recommended above, here's the code I replaced it with:
var controller = DependencyResolver.Current.GetService<LifeCycleEffectsResultsController>();
var result = controller.IndexComparison(model);
return result;
All is working as intended now. Thank you for leading the way.
Solution 8 - C#
I know it's old, but you can:
- Create a service layer
- Move method there
- Call method in both controllers
Solution 9 - C#
Dleh's answer is correct and explain how to get an instance of another controller without missing dependencies set up for IoC
However, we now need to call the method from this other controller.
Full answer would be :
var controller = DependencyResolver.Current.GetService<ControllerB>();
controller.ControllerContext = new ControllerContext(this.Request.RequestContext, controller);
//Call your method
ActionInvoker.InvokeAction(controller.ControllerContext, "MethodNameFromControllerB_ToCall");
Solution 10 - C#
if the problem is to call. you can call it using this method.
yourController obj= new yourController();
obj.yourAction();
Solution 11 - C#
If you use .NET Core or .NET 5 < you can do it like this:
MVC:
services.AddMvc().AddControllersAsServices();
ApiController
:
services.AddControllers().AddControllersAsServices();
Then you can simply inject your controller like any other service