如何注册到控制器ASP.NET MVC当控制器类是在不同的程序集? [英] How to register a Controller into ASP.NET MVC when the controller class is in a different assembly?

查看:247
本文介绍了如何注册到控制器ASP.NET MVC当控制器类是在不同的程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个愚蠢的问题初学者。但是,请多多包涵,因为我试图让很多技巧,我已经知道了的感觉。

I feel like this is a silly beginner question. But, please bear with me as I am trying to make sense of a lot of techniques that I've read about.

我的目标是修改asp.net MVC的控制器系统登录,这样我可以在一个单独的(孩子)装配创建控制器和视图,以及只复制查看文件和DLL文件到主机MVC应用程序和新的控制器是有效的插入到主机应用程序。我不完全知道如何做到这一点。

My goal is to modify asp.net mvc's controller registery so that I can create controllers and views in a separate (child) assembly, and just copy the View files and the DLLs to the host MVC application and the new controllers are effectively "Plugged In" to the host app. I'm not exactly sure how to accomplish this.

显然,我需要某种形式的IoC模式,但我在亏损是从哪里开始的时候它的时间的实际橡胶,以满足道路。

Obviously, I will need some sort of IoC pattern, but I'm at a loss of where to start when it's time for the actual rubber to meet the road.

我的想法是有一个小孩议会与system.web.mvc引用,然后刚刚开始建立,从控制器继承控制器类:

My thought was to have a child assembly with system.web.mvc referenced and then to just start building controller classes that inherited from Controller:

单独的程序:

using System.Web;
using System.Web.Mvc;

namespace ChildApp
{
    public class ChildController : Controller
    {
        ActionResult Index()
        {
            return View();
        }
    }
}

耶所有罚款和花花公子。但后来我看着修改宿主应用程序的控制器注册表在运行时加载我新的子控制器和我弄糊涂了。也许是因为我需要的C#有更深的了解。

Yay all fine and dandy. But then I looked into modifying the host application's Controller registry to load my new child controller at runtime, and I got confused. Perhaps because I need a deeper understanding of C#.

不管怎样,我想我需要创建一个 CustomControllerFactory 类。于是我开始写一个类,将覆盖 GetControllerInstance()方法。由于我打字,intellisence弹出此设置:

Anyway, I thought I needed to create a CustomControllerFactory class. So I started writing a class that would override the GetControllerInstance() method. As I was typing, intellisence popped this up:

主持人MVC应用程序:

Host MVC Application:

public class CustomControllerFactory : DefaultControllerFactory 
{
    protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
    {
        return base.GetControllerInstance(requestContext, controllerType);
    }
}

现在,在这一点上,我不知所措。我不知道做什么。本来,我打算写一个控制器装载机类像一个服务定位是这样的:

Now, at this point I'm at a loss. I don't know what that does. Originally, I was going to write a "Controller Loader" class like a Service Locator like this:

主持人MVC应用程序:

Host MVC Application:

public class ControllerLoader
{
    public static IList<IController> Load(string folder)
    {
        IList<IController> controllers = new List<IController>();

        // Get files in folder
        string[] files = Directory.GetFiles(folder, "*.plug.dll");
        foreach(string file in files)
        {
            Assembly assembly = Assembly.LoadFile(file);
            var types = assembly.GetExportedTypes();
            foreach (Type type in types)
            {
                if (type.GetInterfaces().Contains(typeof(IController)))
                {
                    object instance = Activator.CreateInstance(type);
                    controllers.Add(instance as IController);
                }
            }
        }
        return controllers;
    }
}

然后我打算使用我的控制器列表中的控制器工厂登记。但是......怎么样?我觉得我在计算出来的边缘。我想,这一切BOIS到这个问题:我如何钩到返回base.GetControllerInstance(RequestContext的,controllerType); ?或者,我应该使用不同的方法完全?

And then I was planning on using my list of controllers to register them in the controller factory. But ... how? I feel like I'm on the edge of figuring it out. I guess it all bois down to this question: How do I hook into return base.GetControllerInstance(requestContext, controllerType);? Or, should I use a different approach altogether?

感谢您的帮助。很抱歉,这是如此长篇大论。

Thanks for any help. Sorry this was so long winded.

推荐答案

从根ASP.NET MVC项目中引用的其他装配。如果控制器是在另一个命名空间,则需要修改路由在Global.asax中,像这样的:

Reference the other assembly from the 'root' ASP.NET MVC project. If the controllers are in another namespace, you'll need to modify the routes in global.asax, like this:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new[] { typeof(HomeController).Namespace }
    );

}

注意额外的命名空间参数传递给图路线方法。

这篇关于如何注册到控制器ASP.NET MVC当控制器类是在不同的程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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