8

In a ASP.NET MVC 3 application with areas (see schema below). The "Controllers" directory from the root has been deleted.

When I do this :

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

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: MyProject.Areas.Administration.Controllers.HomeController MyProject.Areas.BackEnd.Controllers.HomeController MyProject.Areas.FrontEnd.Controllers.HomeController

When I do this :

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

I get tis error :

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml


---- Areas
       |
       |---- Administration
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- FrontEnd
       |       |--- Controllers
       |       |      |---- HomeController
       |       |--- Views
       |              |--- Index
       |---- BackEnd
               |--- Controllers
               |      |---- HomeController
               |--- Views
                      |--- Index

Update1 To start to a specific controller in the Areas, I tried this :

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.BackEnd.Controllers" }
    );
}
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

1 Answers1

15

Try the following:

~/Global.asax.cs:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Controllers" }
    );
}

~/Areas/Administration/AdministrationAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Administration_default",
        "Administration/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.Administration.Controllers" }
    );
}

~/Areas/FrontEnd/FrontEndAreaRegistration.cs:

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "FrontEnd_default",
        "FrontEnd/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional },
        new[] { "MyProject.Areas.FrontEnd.Controllers" }
    );
}

Now when you request /Administration/Home/Index, the Index action of HomeController in the Administration area will be invoked and it will look for the ~/Areas/Administration/Views/Home/Index.cshtml view. Make sure this view is present at this location. In your picture you seem to have omitted the Home directory - ~/Areas/Administration/Views/Index.cshtml.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Looks ok. One more think. When I hit F5 in VS, I'd like by default going to Backend area in Home/Index – TheBoubou Feb 05 '12 at 08:23
  • @Kris-I, in this case either modify your area route registration or go to the properties of your project and in the Web tab you could set a startup page in which you could define `Administration/Home/Index`. This way when you run the application it will automatically navigate to this controller. – Darin Dimitrov Feb 05 '12 at 08:30
  • Change startup page ok, but by route registration take a look at update1. Thanks – TheBoubou Feb 05 '12 at 09:09
  • @Kris-I, why are you modifying your Global.asax? It's `AdministrationAreaRegistration` that you should modify. Do you see that inside it all routes must begin with the `Administration` prefix? If you don't change that you won't be able to automatically route a request such as `/` to `/Administration/Home/Index`. Also I don't think that you could have a request such as `/` map to an area unless you play with a custom view engine: http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc – Darin Dimitrov Feb 05 '12 at 09:15
  • it's not a mistake in your code ? both you change the AdministrationAreaRegistration.cs in the second code it's FrontEnd but you write MyProject.Areas.Administration.Controllers – TheBoubou Feb 05 '12 at 09:37
  • @Kris-I, yes, it was a typo. I have fixed it. – Darin Dimitrov Feb 05 '12 at 09:42
  • It's strange when I do http://localhost:11582/FrontEnd/Home or with BackEnd it's ok no problem but with http://localhost:11582/Administration/Home not work. – TheBoubou Feb 05 '12 at 09:56
  • @Kris-I, yes it's strange. I have tested my code and it worked as expected, so maybe you have some difference. – Darin Dimitrov Feb 05 '12 at 09:57
  • strange with "Management" it's ok. – TheBoubou Feb 05 '12 at 10:15