views:

39

answers:

1

Hello,

This must be a trivial question to some, but I haven't found any actual information about this.

I have an ASP.NET MVC web application. As I like to write reusable code, I'd like to put my Controllers into a separate library assembly and reuse them across multiple web applications.

The question is, how can I tell ASP.NET MVC that it should look for controllers in the other assembly instead of the web application itself?

+3  A: 

You need to create your own ControllerFactory.

Check out this link

Basically, create the ControllerFactory as described above, then in your Global.asax, add this line:

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

Which tells ASP.NET MVC to use your ControllerFactory.

The kicker is when you define your routes, you need to give ASP.NET MVC a "hint" as to where this assembly is located:

yourRoute.DataTokens = new RouteValueDictionary(
    new
    {
         namespaces = new[] { "SomeAssembly.Controllers" }
    });

If it doesnt find it, then it looks in the regular namespace (Web Application).

Here is another link showing how to create a Custom Controller Factory.

Good luck!

RPM1984
Thank you, this is exactly what I was looking for. Actually, simply specifying the namespace in which my controllers reside was enough to do the trick!
Venemo
Cool. I've never tried the whole namespace thing, but i currently use Controller factories for dependency injection. Glad it worked.
RPM1984