加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 控制器没有从不同的程序集加载?

发布时间:2020-12-16 03:27:09 所属栏目:asp.Net 来源:网络整理
导读:我正试图从不同的程序集加载一个控制器,但我被困在这里. 我有两个项目: Web应用程序 类库 在Web应用程序中,我定义了一个Route: routes.MapRoute("Root","root/{Action}",new {Controller = "Root",Action = "Index" }); 现在当我点击以下URL时,路由匹配,但
我正试图从不同的程序集加载一个控制器,但我被困在这里.

我有两个项目:

> Web应用程序
>类库

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

routes.MapRoute("Root","root/{Action}",new {Controller = "Root",Action = "Index" });

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

http://webapp/root

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

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??
    }
}

我还尝试在路由中添加名称空间属性:

routes.MapRoute("Root",new {
                Controller = "Root",Action = "Index",Namespaces = new[] { typeof(RootController).Namespace }                     
              });

解决方法

经过多次调试和挫折后,我发现控制器类被声明为私有,但在场景中不起作用.

即使我的ControllerFactory在同一名称空间中,创建控制器缓存的类也不是.因此,由DefaultControllerFactory基类生成的GetControllerInstance请求无法找到RootController类.

将RootController声明为公共解决了这个问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读