Wednesday, November 30, 2011

MVC Asp.Net controllers on multiple assemblies

Today I wrote some sort of "plug-in" for an ASP.NET MVC web application.
It was simply a controller with few action methods.
I really like the idea of separate the controller on a new assembly because it was some sort of "one-shot-use" controller, and soon will death.

But an unexpected problem pop-up at runtime: the MVC controller factory was unable to create the controller: "The IControllerFactory 'MyControllerFactory'did not return a controller for the name 'MyController'".
Looking on the web I wont be able to find any solution for my case.

Finally I dig deep into the MVC source (Asp.NET MVC is realesed as open-source) and I finally find the trick. When started, the controller factory, scan all classes seeking for controllers on all *loaded* assemblies: so to make it works, you have to load the assembly before the controller factory starts the scan.
You can do it as you like: my solution was to use a module that dynamically load ninject kernel modules.

Don't forget to write the right name for the controller: it must end with "Controller" ;-) .

Happy coding ^^