找到多个与名为“ Home”的控制器匹配的类型
本站寻求有缘人接手,详细了解请联系站长QQ1493399855
本文翻译自:Multiple types were found that match the controller named 'Home'
I currently have two unrelated MVC3 projects hosted online. 我目前有两个不相关的MVC3项目在线托管。
One works fine, the other doesn't work, giving me the error: 一个工作正常,另一个不工作,给我错误:
Multiple types were found that match the controller named 'Home'. 找到了多个与名为“ Home”的控制器匹配的类型。 This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. 如果为该请求提供服务的路由('{controller} / {action} / {id}')未指定名称空间来搜索与该请求匹配的控制器,则可能会发生这种情况。
If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter. 如果是这种情况,请通过调用带有“名称空间”参数的“ MapRoute”方法的重载来注册此路由。
The way my hoster works is that he gives me FTP access and in that folder I have two other folder, one for each of my applications. 托管人的工作方式是,他为我提供FTP访问权限,在该文件夹中,我还有另外两个文件夹,每个文件夹用于我的每个应用程序。
ftpFolderA2/foo.com ftpFolderA2 / foo.com
ftpFolderA2/bar.com ftpFolderA2 / bar.com
foo.com works fine, I publish my application to my local file system then FTP the contents and it works. foo.com正常运行,我将应用程序发布到本地文件系统,然后通过FTP传输内容,并且可以正常工作。
When I upload and try to run bar.com, the issue above fires and prevents me from using my site. 当我上传并尝试运行bar.com时,上述问题触发,并阻止了我使用我的网站。 All while foo.com still works . 尽管foo.com仍然有效 。
Is bar.com searching from controllers EVERYWHERE inside of ftpFolderA2 and that's why it's finding another HomeController
? bar.com是否在ftpFolderA2内的任何地方从控制器进行搜索,这就是为什么它正在寻找另一个HomeController
的原因? How can I tell it to only look in the Controller folder as it should? 我怎样才能告诉它只应在Controller文件夹中查找?
Facts: 事实:
- Not using areas. 不使用区域。 These are two COMPLETELY unrelated projects. 这是两个完全不相关的项目。 I place each published project into each respective folder. 我将每个已发布的项目放入每个相应的文件夹中。 Nothing fancy. 没有什么花哨。
- Each project only has 1 HomeController. 每个项目只有1个HomeController。
Can someone confirm this is the problem? 有人可以确认这是问题吗?
#1楼
参考:https://stackoom.com/question/Wu8b/找到多个与名为-Home-的控制器匹配的类型
#2楼
Another solution is to register a default namespace with ControllerBuilder. 另一个解决方案是向ControllerBuilder注册默认名称空间。 Since we had lots of routes in our main application and only a single generic route in our areas (where we were already specifying a namespace), we found this to be the easiest solution: 由于我们的主应用程序中有很多路由,而在我们的区域(我们已经指定了名称空间)中只有一条通用路由,因此我们发现这是最简单的解决方案:
ControllerBuilder.Current.DefaultNamespaces.Add("YourApp.Controllers");
#3楼
Watch this... http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas 观看此视频... http://www.asp.net/mvc/videos/mvc-2/how-do-i/aspnet-mvc-2-areas
Then this picture (hope u like my drawings) 然后这张照片(希望你喜欢我的画)
#4楼
Here is another scenario where you might confront this error. 这是您可能会遇到此错误的另一种情况。 If you rename your project so that the file name of the assembly changes, it's possible for you to have two versions of your ASP.NET assembly, which will reproduce this error. 如果重命名项目以使程序集的文件名更改,则可能有两个版本的ASP.NET程序集,它们将重现此错误。
The solution is to go to your bin
folder and delete the old dlls. 解决方法是转到您的bin
文件夹并删除旧的dll。 (I tried "Rebuild Project", but that didn't delete 'em, so do make sure to check bin
to ensure they're gone) (我尝试过“重建项目”,但是并没有删除它们,因此请务必检查bin
以确保它们消失了)
#5楼
You can also get the 500 error if you add your own assembly that contains the ApiController by overriding GetAssemblies of the DefaultAssembliesResolver and it is already in the array from base.GetAssemblies() 如果您通过覆盖DefaultAssembliesResolver的GetAssemblies来添加自己的包含ApiController的程序集,并且它已经位于base.GetAssemblies()的数组中,则也会出现500错误。
Case in point: 例子:
public class MyAssembliesResolver : DefaultAssembliesResolver
{public override ICollection<Assembly> GetAssemblies(){var baseAssemblies = base.GetAssemblies();var assemblies = new List<Assembly>(baseAssemblies);assemblies.Add(Assembly.GetAssembly(typeof(MyAssembliesResolver)));return new List<Assembly>(assemblies);}
}
if the above code is in the same assembly as your Controller, that assembly will be in the list twice and will generate a 500 error since the Web API doesn't know which one to use. 如果以上代码与Controller在同一程序集中,则该程序集将在列表中出现两次,并会产生500错误,因为Web API不知道使用哪个程序集。
#6楼
There might be another case with Areas even you have followed all steps in routing in Areas(like giving Namespaces in global routing table), which is: 即使您遵循了在区域中进行路由的所有步骤(例如在全局路由表中提供命名空间), 区域也可能存在另一种情况,即:
You might not have wrapped your Global Controller(s) in 'namespace' you provided in routing. 您可能没有将全局控制器包装在路由中提供的“名称空间”中。
Eg: 例如:
Done this: 完成此操作:
public class HomeController : Controller
{
Instead of: 代替:
namespace GivenNamespace.Controllers
{public class HomeController : Controller{