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

fib*_*ics 14 c# asp.net-mvc asp.net-mvc-routing asp.net-mvc-3

我的项目有两个方面.现在,当我运行程序时,我收到此错误:

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  
Run Code Online (Sandbox Code Playgroud)

我在这里找到了一些来源:多个控制器名称
但我认为它只适用于一个区域.
就我而言,我在不同领域有两个项目.希望有人能告诉我该怎么做才能解决问题.
这是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
            );
        }  
Run Code Online (Sandbox Code Playgroud)

顺便说一下,我还有文件夹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" }
            );
Run Code Online (Sandbox Code Playgroud)

提前致谢!!

fib*_*ics 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" }
    );
}
Run Code Online (Sandbox Code Playgroud)

  • 只是在... ...(我知道它已经晚了一年),但我收到了这个错误,因为我更改了输出程序集的名称和我的默认名称空间.我将旧编译的程序集(来自MVC3站点)留在项目的debug/bin文件夹中,却没有意识到.当站点加载时,它从旧的程序集和新重命名的程序集中拾取了主控制器,并抛出了相同的错误.长话短说,我走进垃圾箱,拆除旧装配,一切都开始完美. (28认同)
  • 这个评论肯定是这个的默认答案.ASP.NET MVC 5仍然适用. (2认同)

Jus*_*son 7

在重命名我的项目命名空间/程序集后,我收到此错误.

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