Reputation: 19107
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?
Upvotes: 2
Views: 212
Reputation: 73123
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!
Upvotes: 3