Multiple types were found that match the controller named 'Home' - In two different Areas

限于喜欢 提交于 2019-12-18 19:00:35

问题


I have two areas in my project. Now when I run the program I get this error:

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  

I have found some source here: Multiple Controller name
But I think it only works for one area.
In my case I have two projects in different areas. Hope someone could tell what should I do to solve the problem.
Here is the Global.asax file:

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

By the way, I have also controller ("HomeController") outside the Area folder. This just provides links to two projects BaseAdmin and TitomsAdmin.

I have tried this solution, but still doesn't work:

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

Thanks in advance!!


回答1:


I don't know what happen, but this code works fine:

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



回答2:


I got this error after doing a rename of my project namespace/assembly.

If you renamed the Namespace/Assembly you might have a leftover assembly/dll from the previous name in your bin folder. Just delete it from there and it should work.




回答3:


Right click the project and select clean the project. Or else completely empty the bin directory and then re-build again. This should clear of any left over assemblies



来源:https://stackoverflow.com/questions/8788014/multiple-types-were-found-that-match-the-controller-named-home-in-two-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!