【问题标题】:Multiple types were found that match the controller named 'Home' - In two different Areas找到了与名为“Home”的控制器匹配的多种类型 - 在两个不同的区域中
【发布时间】:2012-01-09 11:53:48
【问题描述】:

我的项目中有两个领域。现在,当我运行程序时,出现此错误:

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. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers:
BaseAdminMVC.Areas.BaseAdmin.Controllers.HomeController
BaseAdminMVC.Areas.TitomsAdmin.Controllers.HomeController  

我在这里找到了一些来源:Multiple Controller name
但我认为它只适用于一个领域。
就我而言,我在不同领域有两个项目。希望有人能告诉我应该怎么做才能解决这个问题。
这是Global.asax 文件:

public static void RegisterRoutes(RouteCollection routes)
        {
            string[] namespaces = new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers", "BaseAdminMVC.Areas.TitomsAdmin.Controllers"};

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces
            );
        }  

顺便说一句,我在Area 文件夹之外还有控制器(“HomeController”)。这只是提供了两个项目BaseAdminTitomsAdmin 的链接。

我已经尝试过这个解决方案,但仍然不起作用

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "BaseAdmin",
                "BaseAdmin/{controller}/{action}",
                new { controller = "Account", action = "Index" },
                new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers" }
            );

            routes.MapRoute(
                "TitomsAdmin",
                "TitomsAdmin/{controller}/{action}",
                new { controller = "Home", action = "Index" },
                new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
            );

提前致谢!!

【问题讨论】:

  • 重新编辑:您必须将默认路线向下移动(到最后一个位置)。顺序在这里很重要。
  • @HenkHolterman 还是没用。
  • 目前还不清楚“不起作用”是什么意思。
  • 是的。实际上我已经解决了这个问题,但我不知道解决方案是如何成为可能的,而第一个解决方案最有可能但没有奏效。好吧,你说得对,顺序很重要。感谢您的帮助!

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-mvc-routing


【解决方案1】:

我不知道发生了什么,但是这段代码可以正常工作:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
    );
}

【讨论】:

  • 刚刚加入..(我知道已经晚了一年),但我收到了这个错误,因为我更改了输出程序集的名称和默认命名空间。我将旧的编译程序集(来自 MVC3 站点)留在了项目的 debug/bin 文件夹中而没有意识到。当站点加载时,它会从旧程序集和新重命名的程序集中获取主控制器,并抛出与您相同的错误。长话短说,我进入垃圾箱,移除了旧组件,一切都开始正常工作了。
  • 谢谢!我现在正在从 4.0 迁移到 .Net 4.5。如果再次出现同样的错误,您的提示可能会有所帮助。
  • @OFConsulting 我知道我不应该在评论中说“谢谢”,但是 FU--*- THANKS !
  • 当然,此评论需要成为对此的默认答案。 ASP.NET MVC 5 仍然适用。
  • @OFConsulting:您的解决方案对我来说是正确的。尽管我可以通过指定命名空间使其工作,但我宁愿解决根本原因以避免未来的混乱。话虽这么说,如果有人真的需要在同一个命名空间中有两个控制器,那么他们应该使用上面的答案。
【解决方案2】:

重命名项目命名空间/程序集后出现此错误。

如果您重命名了命名空间/程序集,您的 bin 文件夹中可能有一个旧名称的剩余程序集/dll。只需从那里删除它,它应该可以工作。

【讨论】:

    【解决方案3】:

    右键单击项目并选择清理项目。或者完全清空 bin 目录,然后重新构建。这应该清除任何剩余的程序集

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多