tags:

views:

1461

answers:

3

I'm just learning asp.net mvc and I'm trying to figure out how to move my controllers into a separate project. Typically when I have designed asp.net web apps before, I created one project for my models, another for my logic, and then there was the web.

Now that I'm learning asp.net mvc I was hoping to follow a similar pattern and put the models and controllers each into their own separate projects, and just leave the views/scripts/css in the web. The models part was easy, but what I don't understand is how to make my controllers in a separate project be "found". Also, I would like to know if this is advisable. Thanks!

+9  A: 

First of all, it is certainly a good idea to put your model into a separate project. As you've discovered, this is trivial.

Regarding Controllers and Views, I don't see any obvious advantage to separating them for most basic projects, although you may have a particular need to do so in a particular application.

If you do choose to do this, then you will need to tell the framework how to find your controllers. The basic way to do this is by supplying your own ControllerFactory. You can take a look at the source code for the DefaultControllerFactory to get an idea for how this is done. Subtyping this class and overriding the GetControllerType(string controllerName) method may be enough to accomplish what you're asking.

Once you've created your own custom ControllerFactory, you add the following line to Application_Start in global.asax to tell the framework where to find it:

ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());

Update: Read this post and the posts it links to for more info. See also Phil Haack's comment on that post about:

ControllerBuilder.Current.DefaultNamespaces.Add(
    "ExternalAssembly.Controllers");

...which is not a complete solution, but possibly good enough for simple cases.

Craig Stuntz
Thanks Craig! This is exactly what I've been looking for. Does this information even exist on the web? I've been googling all over for it with not much luck. StackOverflow comes through again!
Aaron Palmer
Are you going to be reusing the controllers in another project? If not, then there's no point to doing this.
John Sheehan
I agree, controllers handle user input, manipulate model, then pass data to the view. They are typically very specific to an application. Any logic that isn't specific to the app might be better off in a library or the model. But controllers in general ought to be with the web project.
Haacked
I have an error controller and views that are identical between two apps. It makes sense to me to have those in a single assembly that both apps can use.
Sailing Judo
Is it not easier to test the controllers and use IoC when the controllers are in a separate project - that would be the main reason
Chev
@Chev, I don't think it makes any difference there. The testability of your controllers has more to do with *how you code them*, not *where they live.*
Craig Stuntz
+1  A: 

Now (thanks to Craig Stuntz) that I have a clue about what I'm looking for (ControllerFactory), I was able to find this great article about developing a custom ControllerFactory.

http://geekswithblogs.net/sankarsan/archive/2008/12/16/asp.net-mvc---developing-custom-controllerfactory.aspx

Aaron Palmer
A: 

Here is a great screencast on injecting controllers using the Unity framework. One advantage of separating out your controllers is for modularization.

RailRhoad