title: How to register a Controller into ASP.NET MVC when the controller class is in a different assembly?
body: <p>My goal is to modify asp.net mvc's controller registery so that I can create controllers and views in a separate (child) assembly, and just copy the View files and the DLLs to the host MVC application and the new controllers are effectively "Plugged In" to the host app. </p>
  
  <p>Obviously, I will need some sort of IoC pattern, but I'm at a loss.</p>
  
  <p>My thought was to have a child assembly with system.web.mvc referenced and then to just start building controller classes that inherited from <code>Controller</code>:</p>
  
  <p>Separate Assembly:</p>
  
  <pre><code>using System.Web;
  using System.Web.Mvc;
  
  namespace ChildApp
  {
      public class ChildController : Controller
      {
          ActionResult Index()
          {
              return View();
          }
      }
  }
  </code></pre>
  
  <p>Yay all fine and dandy. But then I looked into modifying the host application's Controller registry to load my new child controller at runtime, and I got confused. Perhaps because I need a deeper understanding of C#.</p>
  
  <p>Anyway, I thought I needed to create a <code>CustomControllerFactory</code> class. So I started writing a class that would override the <code>GetControllerInstance()</code> method. As I was typing, intellisence popped this up:</p>
  
  <p>Host MVC Application:</p>
  
  <pre><code>public class CustomControllerFactory : DefaultControllerFactory 
  {
      protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
      {
          return base.GetControllerInstance(requestContext, controllerType);
      }
  }
  </code></pre>
  
  <p>Now, at this point I'm at a loss. I don't know what that does. Originally, I was going to write a "Controller Loader" class like a Service Locator like this:</p>
  
  <p>Host MVC Application:</p>
  
  <pre><code>public class ControllerLoader
  {
      public static IList&lt;IController&gt; Load(string folder)
      {
          IList&lt;IController&gt; controllers = new List&lt;IController&gt;();
  
          // Get files in folder
          string[] files = Directory.GetFiles(folder, "*.plug.dll");
          foreach(string file in files)
          {
              Assembly assembly = Assembly.LoadFile(file);
              var types = assembly.GetExportedTypes();
              foreach (Type type in types)
              {
                  if (type.GetInterfaces().Contains(typeof(IController)))
                  {
                      object instance = Activator.CreateInstance(type);
                      controllers.Add(instance as IController);
                  }
              }
          }
          return controllers;
      }
  }
  </code></pre>
  
  <p>And then I was planning on using my list of controllers to register them in the controller factory. But ... how? I feel like I'm on the edge of figuring it out. I guess it all bois down to this question: How do I hook into <code>return base.GetControllerInstance(requestContext, controllerType);</code>? Or, should I use a different approach altogether?</p>