1

I am trying to separate my MVC project into multiple areas. So i have 3 areas 1) crm 2)services 3) Web. I want PublicWeb to be my default one. that means it should be accessed like www.mysitename.com/mycontroller/myaction( no area name inbetween) and other two to be accessed with the area name (www.mysitename.com/crm/mycontroller/myaction). What routing/ Area configuration i should have ? I tried AreaRegistration.RegisterAllAreas(); and it works only for my default one (web). When i access the other 2, it threw 404 error.

I tried to register indidually like the below one

        var area2reg = new crmAreaRegistration();
        var area2context = new AreaRegistrationContext(area2reg.AreaName, RouteTable.Routes);
        area2reg.RegisterArea(area2context);

        var area1reg = new webAreaRegistration();
        var area1context = new AreaRegistrationContext(area1reg.AreaName, RouteTable.Routes);
        area1reg.RegisterArea(area1context);

Then my publicweb works. But when i access my forum it threw this error,

Multiple types were found that match the controller named 'home'. This can happen if the route that services this request ('crm/{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.

My RegisterArea function for web is this

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "web_default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }

        );
    }

and the one for crm is this

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

How do i handle this ?

2 Answers 2

2

From what I can see the area routes look fine. Did you update the default route in your Global.asax to send requests to the web area?

Something like:

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}",
            new { area = "web", controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

    }
1
  • yes, im tried a lot to use default routing and area together .so this should be the correct solution . Oct 15, 2017 at 14:16
1

I think Jonathan S's solution is definitely worth a try, but you might consider a different approach. That would be to put your web files in the default locations. The routing engine would not look in the Area's for those files when no Area is part of the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.