控制器没有从不同的组件加载?

Rop*_*tah 5 asp.net-mvc routing assemblies controller

我正试图Controller从一个不同的集会加载一个但我被困在这里.

我有两个项目:

  1. Web应用程序
  2. 类库

在Web应用程序中我定义了一个Route:

routes.MapRoute("Root", "root/{Action}", 
                  new {Controller = "Root", Action = "Index" });
Run Code Online (Sandbox Code Playgroud)

现在当我点击以下URL时,路由匹配,但是会引发404错误.任何人都可以告诉我为什么会这样吗?(ps RootController位于类库中)

http://webapp/root
Run Code Online (Sandbox Code Playgroud)

我试过添加一个自定义控制器工厂:

public class ControllerFactory : DefaultControllerFactory {
    protected override IController GetControllerInstance(RequestContext reqContext, Type controllerType)
    {
        // reqContext.Route is correct and has the "Root" route loaded
        // controllerType seems to be null ??

        // if I break execution here and manually set controllerType to
        // typeof(ClassLibrary.RootController) and continue from there,
        // then everything works as expected... 

        // So this method is being called without controllerType... but why??
    }
}
Run Code Online (Sandbox Code Playgroud)

我还尝试namespaces在路线中添加一个属性:

routes.MapRoute("Root", "root/{Action}", 
              new {
                Controller = "Root", 
                Action = "Index", 
                Namespaces = new[] { typeof(RootController).Namespace }                     
              });
Run Code Online (Sandbox Code Playgroud)

Rop*_*tah 4

经过多次调试和挫折后,我发现声明的控制器类private在该场景中不起作用。

即使 my位于同一名称空间中,但创建控制器缓存的ControllerFactory类却不在同一名称空间中。因此,由基类发出的对 的请求无法找到该类。GetControllerInstanceDefaultControllerFactoryRootController

声明RootControlleraspublic可以解决问题。