In a previous post I talked about using MVC’s controller injection to drop in DLLs that contain controller classes. It allows for a nice separation leading onto creating a composite UI. I was happily using this mechanism when it just refused to load a controller from an additional DLL. I still do not understand why it doesn’t work but my workaround is to override the ControllerFactory. Here is a quick example of this, please note I’ve hardcoded the condition for the sake of this example.
protected void Application_Start() { ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory()); AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
public class MyControllerFactory : System.Web.Mvc.DefaultControllerFactory { protected override Type GetControllerType(System.Web.Routing.RequestContext requestContext, string controllerName) { if (controllerName == "Library") { Type reflib = Type.GetType("LibraryUI.Library,LibraryUI"); return reflib; } return base.GetControllerType(requestContext, controllerName); } }
Note: setcontrollerfactory is a bit old school, should be using the IoC of choice…I’ll need to look into a bit more, but for now this works