14

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!!

Community
  • 1
  • 1
fiberOptics
  • 6,955
  • 25
  • 70
  • 105
  • 1
    Re the edit: You have to move the Default route down (to last place). Order is important here. – H H Jan 09 '12 at 20:49
  • @HenkHolterman Still didn't work. – fiberOptics Jan 10 '12 at 01:45
  • And it's still not clear what "doesn't work" means. – H H Jan 10 '12 at 10:59
  • Yes. Actually I have solved the problem but I don't know how the solution became possible while the first solution are most possible but didn't work. Well, you are right that order is important. Thanks for help! – fiberOptics Jan 11 '12 at 01:08

3 Answers3

18

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" }
    );
}
fiberOptics
  • 6,955
  • 25
  • 70
  • 105
  • 28
    Just chiming in.. (and I know it's a year late), but I recieved this error because I changed the name of my output assembly, and my default namespace. I left the old compiled assembly (from the MVC3 site) in the debug/bin folder of the project without realizing it. When the site would load, it picked up the home controllers from both the old assembly, and the newly renamed assembly and threw the same error you got. Long story short, I went into the bin, removed the old assembly, and everything started working perfectly. – Marcus Nov 24 '12 at 05:34
  • Thanks for that! I'm now migrating to .Net 4.5 from 4.0. Your tips could be a help if same error shows again. – fiberOptics Nov 26 '12 at 04:33
  • 1
    @OFConsulting I know that I shouldn't say "Thanks" in a comment, but FU--*- THANKS ! – Ydhem Sep 07 '13 at 15:28
  • 2
    For sure this comment needs to be the default answer to this. Still holds true with ASP.NET MVC 5. – Louie Bacaj Mar 29 '14 at 03:49
  • Also ensure you're viewing all files when doing the above BIN removals. – Steve Woods May 24 '14 at 22:30
  • I actually had to delete my Azure website and re-create a new one. Then, deploy to azure. I tried deleting .dll and updating routeconfig. No luck. But, delete and re-deploy worked. – JoshYates1980 Aug 05 '14 at 18:31
  • 1
    @OFConsulting: Your solution was the correct one for me. Even though I could make it work with specifying the namespace but I would rather to solve the root cause to avoid future confusions. That being said, if someone really needs to have two controllers in same namespace then they should use the above answer. – Aidin Sep 19 '14 at 22:59
7

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.

Justin Ipson
  • 481
  • 9
  • 6
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

VivekDev
  • 20,868
  • 27
  • 132
  • 202