99

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!

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
mattruma
  • 16,589
  • 32
  • 107
  • 171
  • Refer this http://stackoverflow.com/a/12633541/2089963. It works for me – Syed Mohamed Mar 10 '16 at 08:40
  • This happen to me when I renamed the project/assembly, I just deleted the bin folder and recompile the project and it worked fine. – Core May 30 '16 at 11:06

13 Answers13

172

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.

David Ruttka
  • 14,269
  • 2
  • 44
  • 39
  • 1
    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" }` – David Ruttka Jan 09 '12 at 14:56
  • 2
    That namespace pattern didn't work for me. `MyProject.Controllers` did however. – The Muffin Man Feb 09 '12 at 06:36
  • I tried this one. But the index in this `MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController`class is executing. But the View appears is the old one. – kbvishnu Jan 22 '13 at 05:36
  • 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. http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/e6ea9683f1db#src/System.Web.Mvc/RouteCollectionExtensions.cs – Chris Moschini Jan 23 '13 at 22:09
  • 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: http://bubblogging.wordpress.com/2012/06/09/mvc-routing-namespaces/ (bottom of page) – Chris Moschini Jan 23 '13 at 23:29
  • @jenson-button-event everyone loves some [stringly typed](http://blog.codinghorror.com/new-programming-jargon/) data – Liam Aug 21 '15 at 09:37
  • 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. – David Ruttka Aug 23 '15 at 00:24
  • Instead of entering a hard coded namespace string you can do this: namespaces: new[] { typeof(HomeController).Namespace } – Pedro May 25 '17 at 19:52
30

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" }
        );
cooloverride
  • 301
  • 3
  • 2
  • 3
    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 Jun 21 '13 at 17:55
  • 13
    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! – Yann Duran Jun 21 '13 at 18:13
  • @YannDuran, ran into a similar problem and your fixed helped. Thanks. – Klaus Nji Aug 03 '13 at 03:32
13

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

Reynaldo
  • 504
  • 8
  • 13
3

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
    }
Leonel Sanches da Silva
  • 6,972
  • 9
  • 46
  • 66
2

Another plausible cause of this issue could be found below:

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

Community
  • 1
  • 1
T Gupta
  • 947
  • 11
  • 11
2

use this

routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "ProjectName.Controllers" }
        );
Ben
  • 51,770
  • 36
  • 127
  • 149
cracker
  • 4,900
  • 3
  • 23
  • 41
1

Use only the name of the project:

Public Class RouteConfig
    Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
        routes.MapRoute( _
            name:="Default", _
            url:="{controller}/{action}/{id}", _
            defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
           , namespaces:={"MvcAreas"})  
    End Sub
allen213
  • 2,267
  • 2
  • 15
  • 21
glownet
  • 11
  • 1
1

I had the same issue and found that the older version had created compiled files in the "bin" folder.

Once I deleted these the error disappeared.

0

As Chris Moschini mention the namespaces parameter may not be enough if you have two areas with same controller name with different namespaces and the default none area route will return 500 server error.

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

It "best" to override the default route handler and add this line:

RequestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;
XzaR
  • 610
  • 1
  • 7
  • 17
0

I had this issue after I added a reference to another project that had the same routes and the problem continued after I removed the reference.

Resolved by deleting the .dll file of that added reference from the bin folder and rebuilding.

0

Like many others, I had this problem after creating a new MVC template project from VS2017 menu, on building the project I would get the op's error message. I then used the answer https://stackoverflow.com/a/15651619/2417292 posted earlier in this thread by cooloverride for mvc4 projects. This still didn't fix my issue so I renamed my Home view folder and HomeController file to be the view folder Company/ and controller file CompanyController. This then worked for me, not a fix per say but a workaround if your not stuck on having the route Home/Index my other issue was I couldn't find the reference causing the error and due to my dev platform being Azure WebApplication vs a full VM with a complete file system and IIS settings to mess with.

0

For MVC5 below code works for issue same controller name for different areas. You have to specify which area controller have to hit first.

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
             new[] { "ApplicationName.Areas.AreaName.Controllers" }
        ).DataTokens.Add("area", "AreaName");
0

This issue occurred when I accidentally added

[Route("api/[controllers]s")]

instead of

[RoutePrefix("api/[controllers]s")]

to the top of my ApiController.

Esaith
  • 706
  • 9
  • 12