添加控制器工厂ASP MVC [英] Adding a controller factory to ASP MVC

查看:160
本文介绍了添加控制器工厂ASP MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作的大型项目的设计理念,我想我有它想通了,但更喜欢得到在一些反馈),一般的想法,和b)我提出的实现。

I have a design idea for a large project at work and I think I have it figured out but would really love to get some feedback on a) the idea in general, and b) my proposed implementation.

基本的想法很简单:我想创建一个可以在未来与更多的控制器和视图,而无需重新编译code扩展的ASP MVC应用程序。这个想法是有一个MVC应用程序有一个非常基本的功能集,然后通过添加包含控制器,数据和业务逻辑,特定于该应用程序的另一个'Application.dll扩展功能。该意见将被简单地复制到同一目录作为主要的MVC应用程序在安装过程中。

The basic idea is simple: I want to create an ASP MVC application that can be extended in the future with additional controllers and views without having to recompile the code. The idea is to have one MVC application with a very basic set of features and then extend the functionality by adding another 'Application.dll" that contains controllers, data, and business logic that are specific to that application. The views will simply be copied into the same directory as the main MVC application during install.

问题是MVC做对的类型路由相同的组件内所以即使我谨路由定义到数据库中,MvcHttpHandler将无法路由任何新的DLL,因为它不知道在它的控制器类型。纵观MVC code,我发现加载它们简单地调用Activator.CreateInstance看起来只有在当前汇编控制器。

The problem is that MVC does its routing on types within the same assembly so even if I move the routing definitions to the database, the MvcHttpHandler would not be able to route anything to the new Dll since it doesn't "know" the controller types in it. Looking at the MVC code, I found that to load the controllers they are simply calling Activator.CreateInstance which looks only in the current assembly.

我的解决方法很简单,但也许我失去了一些东西:我会通过直接更换的ControllerFactory覆盖MvcHttpHandler(不知道如何做到这一点),或在派生类中复制该功能。新的code将读取请求,并尝试从扩展的人首先从当前程序集,然后加载控制器。一旦正确的组装被发现,我将使用的CreateInstance和装配传递给它来得到我想要的控制器。

My solution is simple but maybe I'm missing something: I will override the MvcHttpHandler by either replacing the ControllerFactory directly (not sure how to do that) or by duplicating that functionality in a derived class. The new code will read the request and try to load the controller first from the current assembly and then from the extended ones. Once the proper assembly is found, I will use CreateInstance and pass that assembly to it to get the controller I want.

希望这是有道理的。

在此先感谢!

推荐答案

的<一结束href=\"http://weblogs.asp.net/shijuvarghese/archive/2008/10/10/asp-net-mvc-tip-dependency-injection-with-structuremap.aspx\">this文章展示了如何实现自己的ControllerFactory。基本上,你从DefaultControllerFactory派生再用线它在的Application_Start()在Global.asax中。

The end of this article shows how to implement your own ControllerFactory. Basically, you derive from DefaultControllerFactory and then wire it up in Application_Start() in your global.asax.

这篇关于添加控制器工厂ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆