无参数构造函数、结构图、新区域

发布于 2024-12-20 06:03:08 字数 229 浏览 1 评论 0原文

我知道错误“没有为此对象定义无参数构造函数”已被询问大约一百万次。我的情况有所不同,

我有一个可用的应用程序。许多控制器和一个有许多控制器的区域。我刚刚添加了一个新区域。我添加了一个控制器,然后添加了该控制器的链接。现在我明白了 “没有为此对象定义无参数构造函数”错误

我以前见过并解决了这个问题,但它实际上只每 5 个月发生一次。每次我都完全忘记(压抑)答案。

请帮助

拉伊夫

I know the error "No parameterless constructor defined for this object" has been asked about a million times. My situation is different

I have a working app. Many many controllers and one area with lots of controllers. I just added a new area. I added a controller and then a link to that controller. NOW I get the
"No parameterless constructor defined for this object" error

I have seen and conquered this problem before but it really only happens like once every 5 months. And everytime I have totally forgotten ( repressed ) the answer.

Please help

Raif

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏辞 2024-12-27 06:03:08

好的,看来有几个原因可能会导致此错误。毫不奇怪,它们中没有一个与没有无参数构造函数有关。我知道的两个问题是

:1)如果您正在使用一个区域,并且将控制器从一个命名空间移动到新的命名空间,并且不更新命名空间以反映该区域,您将收到此错误。

2)这就是我现在的情况,如果你向控制器的构造函数中注入一些东西,并且你注入的项目有问题(没有实例化,或者它没有在你的IOC注册中注册或其他一些运行时错误) )你会得到这个错误。

如果人们能想到其他的,他们应该把它们列在这里,因为我认为还有几个导致错误的原因。

Ok, so it seems that there are several reasons one might get this error. Not surprisingly none of them have @#$% all to do with not having a parameterless constructor. The two that I'm aware of are

1) if you are using an area and you, say, move a controller from one namespace to the new one and don't update the namespace to reflect the area you will get this error.

2) and this is my situation now, if you are injecting something into the constructor of the controller and the item you are injecting has a problem with it ( there are no instantiations, or it's not registered in your IOC registration or some other runtime error ) you will get this error.

If people can think of others they should list them here because I think there are several more causes for the error.
R

旧梦荧光笔 2024-12-27 06:03:08

虽然我意识到这并不能真正回答您的问题,但您的回答对我的故障排除工作有所帮助。

我最近在使用 MVC Contrib 项目中的 MVC Portable Areas 时遇到了同样的问题。我发现在 IOC 注册期间扫描程序集时,还必须包括可移植区域的任何 dll 依赖项,大致如下:

ObjectFactory.Initialize(x => x.Scan(y =>
    {
        y.Assembly("PortableAreaAssemblyName");
    }));
ObjectFactory.Configure(x =>
    {
        x.For<IClassInterfaceUsedByControllerConstructor>().Use<IntendedClassInstance>();
    });

While I realize this doesn't truly answer your question, your answer helped me in my troubleshooting endeavors.

I just recently came across this same issue while using MVC Portable Areas from the MVC Contrib project. I found that any dll dependancies the portable areas had must also be included when scanning for assemblies during IOC registration, something along the lines of this:

ObjectFactory.Initialize(x => x.Scan(y =>
    {
        y.Assembly("PortableAreaAssemblyName");
    }));
ObjectFactory.Configure(x =>
    {
        x.For<IClassInterfaceUsedByControllerConstructor>().Use<IntendedClassInstance>();
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文