开发者

How to tell ASP.NET MVC to look for controllers in a library?

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?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜