189

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();
}
6
  • 4
    I know this question is old but in my opinion you should mark the answer from ed chapel as the best one, tieson's looks like a hack, it's still valid, but why use a workaround when you can use it the way it was meant to be and get the desired result
    – Anders M.
    Feb 28, 2014 at 14:00
  • 2
    @AndersM. Ed's answer does a redirect. That is not what I want when I found this question searching for a solution.
    – mxmissile
    Dec 12, 2014 at 17:07
  • @mxmissile not to be a dick but Ed's answer is what the asker needs since he wants a view that is returned based on what is uploaded, I agree that asker could have done a better job at formulating his question(is this the right word?) we can't know this though as his english may be limited, even though Tiesons answer helped you - which is good - it doesn't change the fact that Ed's answer best reflects what the asker needs
    – Anders M.
    Dec 16, 2014 at 15:16
  • 2
    @AndersM. I understand, my comment wording was just bad... :-) I should have emphasized the point that was not the result I desired.
    – mxmissile
    Dec 16, 2014 at 16:30
  • 1
    @AndersM. The asker accepted the answer of Tieson as best, so I'm not sure why you would decide for him? The answer Tieson gave me helped me more then the answer Ed's answer. SO is not just for helping a single person, but everyone who has similar problems. So why not just keep Tieson's answer on top?
    – Kevin
    Dec 19, 2014 at 13:57

11 Answers 11

254

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);
10
  • Exactly what I was looking for. Note that those not using IoC still won't get an HttpContext injected.
    – brichins
    Aug 28, 2015 at 21:34
  • var controller would be assigned the type ControllerB, yes.
    – DLeh
    Feb 2, 2016 at 14:58
  • 1
    This gets me close, but one problem that arises is that in my case, controller.MyAction() makes reference to User.Identity which appears to be uininstantiated. May 25, 2016 at 17:10
  • 1
    @ilasno I'm rusty on MVC these days, but I think I meant that you have to actually have IoC set up to get a fully populated Controller object (e.g. an associated HttpContext). I believe I used this approach without any IoC to get a "shallow" controller object (just needed access to certain functionality) and was initially confused about why parts were "missing". [aside: I worked around it while still using this approach, but probably should have refactored that functionality out to a shared class.] As for IoC setup and choices, I'd have to refer you to other articles / SO questions.
    – brichins
    Dec 12, 2016 at 19:59
  • 3
    Some people get carried away with pointless edits... note that someone edited the answer changing the variable "controller" to "ctrlr"... so it should read "ctrlr.ControllerContext = new ControllerContext(this.Request.RequestContext, ctrl);" if that user edited it correctly
    – JoeSharp
    Dec 27, 2017 at 15:10
124

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");

15
  • 93
    Won't you be missing ControllerContext, Request and friends if you just do this?
    – cirrus
    Oct 15, 2013 at 10:37
  • 30
    The instantiation of controller is not a good idea because it's life cycle might be controlled by another part of the application. E.g. when using an IoC container all depdencies should be injected, etc. Nov 25, 2013 at 16:37
  • 57
    If your using IoC, you can get a populated controller via var controller = DependencyResolver.Current.GetService<ControllerB>();
    – mxmissile
    Dec 12, 2014 at 17:11
  • 9
    @mxmissile That's worth adding as a new answer, rather than a comment here.
    – Tieson T.
    Dec 13, 2014 at 3:15
  • 3
    @ilasno Are you familiar with the term "inversion of control"? The point he's making is that if you have components in your controllers that need to be injected into the constructor, my answer doesn't really work, unless you use something like the DependencyResolver as a service-locator.
    – Tieson T.
    Dec 11, 2016 at 18:26
64

Your sample looks like psuedo code. You need to return the result of RedirectToAction:

return RedirectToAction("B", 
                        "FileUploadMsgView",
                        new { FileUploadMsg = "File uploaded successfully" });
2
  • 5
    It must be pointed out that if the target action accepts POST only, this won't work. Jul 25, 2014 at 19:09
  • 18
    This returns a 302 which causes another hit to the server which is not what the question asks.
    – rboarman
    Feb 17, 2015 at 0:42
17

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)

4
  • 6
    If you try to call an action in the controller which uses View(..) or PartialView(...) you need to manually change the routeData, so that ASP.NET knows how to find your view. controller.RouteData.Values["controller"] = "Home";controller.RouteData.Values["action"] = "Index"; Assuming you are trying to return the result from the Index action in HomeController.
    – Steven
    Jun 18, 2016 at 1:28
  • @Steven I had to apply these values to this rather than controller. Ultimately the result comes back through the local controller (this) so that's what ends up trying to find the view.
    – aaaantoine
    Aug 29, 2016 at 13:36
  • 1
    I'd add also that Url property isn't initialized upon DependencyResolver.Current.GetService<ControllerB>(). So you have to copy it from current controller manually.
    – Ralfeus
    Feb 7, 2017 at 10:35
  • In the targeting Action you should use return View("ViewName"); instead just return View();
    – mNejkO
    Mar 12, 2017 at 23:52
13

I know it's old, but you can:

  • Create a service layer
  • Move method there
  • Call method in both controllers
12

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);
    ....
    }

}
2
  • 3
    imo the cleanest answer, but you should set the controller context to the new controller.
    – Mafii
    Jun 26, 2017 at 13:26
  • 2
    Don't forget services.AddMvc().AddControllersAsServices(); in Startup.cs
    – Canada Wan
    Oct 9, 2020 at 20:31
12

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();

2
  • 1
    seems ok for .NET Core 2.1, but does not fit so well for .NET Core 3.1 (and probably upcoming .NET 5) where the controllers are added with AddControllers I am still looking for the cleanest solution for .NET 3.1...
    – EricBDev
    Aug 17, 2020 at 16:24
  • .NET 6 status: builder.Services.AddTransient<myControllerIwantToInject>(); //working builder.Services.AddControllers();//not working
    – The scion
    Jan 24 at 7:25
7

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.

0
6

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");
1
  • 1
    How do you call action "MethodNameFromControllerB_ToCall" if it expects parameters? for example MethodNameFromControllerB_ToCall(int somenum, string sometext)? Dec 13, 2018 at 15:14
6

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

5

if the problem is to call. you can call it using this method.

yourController obj= new yourController();

obj.yourAction();
2
  • 2
    Pfft! What if you're expecting a result from an action instead? var res = new ControllerB().SetUpTimer(new TimeSpan(23, 20, 00));
    – DirtyBit
    Mar 29, 2018 at 9:49
  • Just because they didn't parse the return value doesn't mean it's not a valid answer.
    – Zimano
    Sep 3, 2021 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.