Search code examples
asp.net-mvcasp.net-mvc-routingdirectory-structure

Map a URL to controllers with the same name


I've the following routing:

routes.MapRoute("Home", "{language}-{culture}/{controller}/{action}");

and then I have two controllers like this:

Controllers
--->en-EN
-------->HomeController
--->pt-PT
-------->HomeController

Unfortunately this isn't working and I am getting this error:

"Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{language}-{culture}/{controller}/{action}') 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:

MvcApplication2.Controllers.en_EN.HomeController
MvcApplication2.Controllers.pt_PT.HomeController"

I tried to add the namespace "MvcApplication2.Controllers" as a parameter but it still, but I don´t see how it could have worked, but that was the solution I found on the internet.

I've just started with ASP.Net MVC, so please don't sorry if I missed something very basic.

Thanks a lot.


Solution

  • Are you going to have alot of different cultures? You may want to look into using MVC Areas. They're made for this kind of thing.

    If you only have 2, you could hardcode your routes like this:

    routes.MapRoute(
         "HomeEn", // Route name
         "en-EN/{controller}/{action}", // URL with parameters
         new { controller = "Home", action = "Index" }, // Parameter defaults
         new string[] { "MvcApplication2.Controllers.en_EN"}
    );
    
    routes.MapRoute(
         "HomePt", // Route name
         "pt-PT/{controller}/{action}", // URL with parameters
         new { controller = "Home", action = "Index" }, // Parameter defaults
         new string[] { "MvcApplication2.Controllers.pt_PT"}
    );