发现多个类型与名为“Home”的控制器匹配 - 在两个不同的区域中

时间:2012-01-09 11:53:48

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

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

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
            );
        }  

顺便说一下,我在HomeController文件夹之外还有控制器(“Area”)。这只是提供了两个项目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" }
            );

提前致谢!!

3 个答案:

答案 0 :(得分:18)

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

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" }
    );
}

答案 1 :(得分:6)

在重命名项目命名空间/程序集后,我收到了这个错误。

如果重命名了命名空间/程序集,则可能是bin文件夹中以前名称的剩余程序集/ dll。只需从那里删除它就可以了。

答案 2 :(得分:2)

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