发现多种类型与名为“Home"的控制器相匹配 [英] Multiple types were found that match the controller named 'Home'

查看:30
本文介绍了发现多种类型与名为“Home"的控制器相匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个不相关的 MVC3 项目在线托管.

I currently have two unrelated MVC3 projects hosted online.

一个工作正常,另一个不起作用,给我错误:

One works fine, the other doesn't work, giving me the error:

找到多个与名为Home"的控制器匹配的类型.这如果为该请求提供服务的路由可能会发生('{controller}/{action}/{id}') 没有指定要搜索的命名空间用于匹配请求的控制器.

Multiple types were found that match the controller named '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.

如果是这样,通过调用MapRoute"方法的重载来注册此路由需要一个命名空间"参数.

If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

我的托管人的工作方式是他给我 FTP 访问权限,在该文件夹中我有另外两个文件夹,一个用于我的每个应用程序.

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.

ftpFolderA2/foo.com

ftpFolderA2/foo.com

ftpFolderA2/bar.com

ftpFolderA2/bar.com

foo.com 工作正常,我将我的应用程序发布到我的本地文件系统,然后 FTP 内容并且它工作正常.

foo.com works fine, I publish my application to my local file system then FTP the contents and it works.

当我上传并尝试运行 bar.com 时,上述问题会触发并阻止我使用我的网站.尽管 foo.com 仍然有效.

When I upload and try to run bar.com, the issue above fires and prevents me from using my site. All while foo.com still works.

bar.com 是否从 ftpFolderA2 内的控制器搜索,这就是为什么它要找到另一个 HomeController?我怎样才能告诉它只在 Controller 文件夹中查看它应该的内容?

Is bar.com searching from controllers EVERYWHERE inside of ftpFolderA2 and that's why it's finding another HomeController? How can I tell it to only look in the Controller folder as it should?

事实:

  1. 不使用区域.这是两个完全不相关的项目.我将每个已发布的项目放入各自的文件夹中.没什么特别的.
  2. 每个项目只有 1 个 HomeController.

有人可以确认这是问题吗?

Can someone confirm this is the problem?

推荐答案

当您使用区域并且区域和根内具有相同的控制器名称时,经常会出现此错误消息.例如你有两个:

This error message often happens when you use areas and you have the same controller name inside the area and the root. For example you have the two:

  • ~/Controllers/HomeController.cs
  • ~/Areas/Admin/Controllers/HomeController.cs

为了解决这个问题(正如错误消息所建议的那样),您可以在声明路由时使用命名空间.所以在Global.asax的主路由定义中:

In order to resolve this issue (as the error message suggests you), you could use namespaces when declaring your routes. So in the main route definition in Global.asax:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Controllers" }
);

并在您的 ~/Areas/Admin/AdminAreaRegistration.cs 中:

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new[] { "AppName.Areas.Admin.Controllers" }
);

如果您不使用区域,您的两个应用程序似乎都托管在同一个 ASP.NET 应用程序中,并且会发生冲突,因为您在不同的命名空间中定义了相同的控制器.如果您想避免此类冲突,您必须将 IIS 配置为将这两个作为单独的 ASP.NET 应用程序托管.如果您无权访问服务器,请咨询您的托管服务提供商.

If you are not using areas it seems that your both applications are hosted inside the same ASP.NET application and conflicts occur because you have the same controllers defined in different namespaces. You will have to configure IIS to host those two as separate ASP.NET applications if you want to avoid such kind of conflicts. Ask your hosting provider for this if you don't have access to the server.

这篇关于发现多种类型与名为“Home"的控制器相匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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