Having issue with multiple controllers of the same name in my project

91,319

Solution 1

The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."

routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
     new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);

This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.

Solution 2

This is the asp.net mvc4 approach:

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "RegisterNow", id = UrlParameter.Optional },
            namespaces: new[] { "YourCompany.Controllers" }
        );

Solution 3

I had renamed the namespaces, so, i only delete de folders bin and obj and rebuild, work again.

Solution 4

If you're using RazorGenerator, just informing the namespaces parameter could be not enough.

I got to solve adding the statement marked below at Global.asax.cs:

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        ControllerBuilder.Current.DefaultNamespaces.Add("MyProject.Controllers"); // This one
    }

Solution 5

Another plausible cause of this issue could be found below:

Multiple types were found that match the controller named 'Home'

Share:
91,319
mattruma
Author by

mattruma

I fell in love with computers when I got my first Commodore 64! Check me out on twitter at http://www.twitter.com/mattruma

Updated on November 10, 2021

Comments

  • mattruma
    mattruma over 2 years

    I am running into the following error with my ASP.NET MVC 3 project:

    Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('Home/{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: MyCompany.MyProject.WebMvc.Controllers.HomeController MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController

    I have a HomeController in my default controller folder, with a class name of MyCompany.MyProject.WebMvc.Controllers.HomeController.

    My RegisterRoutes method, in my global.asax, looks like:

        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 } // Parameter defaults
            );
        }
    

    I then have an area called Company, with a HomeController in the default controller folder for the area, with a class name of MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.

    The RegisterArea method in the CompanyAreaRegistration file looks like:

       public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Company_default",
                "Company/{controller}/{action}/{id}",
                new { area = "Company", action = "Index", id = UrlParameter.Optional }
            );
        }
    

    This is all leading the error I highlighted at the beginning of this post. I am struggling trying to piece together a solution from various other posts, with NO LUCK.

    Is it possible to have a HomeController in the default controllers folder and then one in EACH area? If so, do I need to make (assuming I do) changes to my configuration file to make this work?

    Any help would be much appreciated!

  • David Ruttka
    David Ruttka over 12 years
    I haven't tested it, but the namespaces parameter is a string array, so you should be able to pass any number by adding to the array: new string[] { "MyCompany.MyProject.WebMvc.Controllers", "My.Second.Namespace", "My.Third.Namespace", "Namespaces.Etc" }
  • The Muffin Man
    The Muffin Man about 12 years
    That namespace pattern didn't work for me. MyProject.Controllers did however.
  • kbvishnu
    kbvishnu over 11 years
    I tried this one. But the index in this MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeCon‌​trollerclass is executing. But the View appears is the old one.
  • Chris Moschini
    Chris Moschini about 11 years
    Worth nothing that the 4th parameter here can be a bit confusing because of type inference and variable meaning. The 4th parameter can be a string array to constrain by namespace, an object to add IRouteConstraints OR strings that can be interpreted as constraints, or you can have 5 parameters and include both. aspnetwebstack.codeplex.com/SourceControl/changeset/view/…
  • Chris Moschini
    Chris Moschini about 11 years
    Also worth noting a path will find its way to other namespaces by default - if you want to lock a path to a namespace or set of namespaces you have to disable this fallback: bubblogging.wordpress.com/2012/06/09/mvc-routing-namespaces (bottom of page)
  • Yann Duran
    Yann Duran almost 11 years
    thanks, this solved it for me, though I can't figure out for the life of me why my application thinks there are two home controllers in the first place. I did a search & replace of the namespace, which appears to have caused the problem, but a search of the entire solution doesn't show any instances of the rogue namespace.
  • Yann Duran
    Yann Duran almost 11 years
    Well, I found the cause of my problem. Hopefully this may help someone else desperately searching, as I was today. Because I had changed the name of the application (as well as the namespace), there was still a DLL left in the bin folder that didn't get deleted by a clean. There must be some MEF magic going on under the covers. As soon as I discovered & deleted the old DLL, the problem went away. No wonder a text search didn't find it!
  • Klaus Nji
    Klaus Nji over 10 years
    @YannDuran, ran into a similar problem and your fixed helped. Thanks.
  • Liam
    Liam over 8 years
    @jenson-button-event everyone loves some stringly typed data
  • David Ruttka
    David Ruttka over 8 years
    The strings can of course be defined anywhere, including using reflection to grab the namespace. To add that complexity into an answer that simply shows an example of calling an overload seems unnecessary. The community can certainly feel free to edit the answer if they feel that would be helpful.
  • Pedro
    Pedro almost 7 years
    Instead of entering a hard coded namespace string you can do this: namespaces: new[] { typeof(HomeController).Namespace }
  • Hashim Akhtar
    Hashim Akhtar almost 3 years
    Deleting the contents of the "bin" and the "obj" folder did the job for me.