Antoine Meltzheim
Antoine Meltzheim

Reputation: 9934

Areas and routing

I'm quite stuck I might say dispite all other posts found on the site.

My solution has two areas Front and Back, and I don't want to use the default root controllers and views provided by default.

My FrontAreaRegistration.cs is like :

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

My BackAreaRegistration.cs is like :

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "Back_default",
        "Back/{controller}/{action}/{id}",
        new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }
        , new[] { "Show.Areas.Back.Controllers" }
    );
}

And Global.asax like :

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

Getting folowing exception :

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: Show.Areas.Front.Controllers.HomeController Show.Areas.Back.Controllers.HomeController

Problem is I can't reach the Home controller from Front area. Even if correct namespace added to context.MapRoute method overload ...

Any help will be appreciated.

Upvotes: 3

Views: 175

Answers (1)

petro.sidlovskyy
petro.sidlovskyy

Reputation: 5103

The error is raised because you don't specify Area name in your request. Because of that "Default" route (from Global.asax) is matched for request and tries to search "Index" action of "Home" controller. As far as there two matches (for both areas) exceptions is thrown.
There are few ways to fix this. One possible is to modify Global.asax:

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

But in this case "Default" route will work for Front area only.

Upvotes: 1

Related Questions