Search code examples
asp.net-mvcasp.net-routing

Area routing in ASP.NET MVC


I am a bit confused with the area routing. I created an area called Backbone. I have my default controller, views and models as well.

http://localhost:46870/ 

gives me the following 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:
LearnJavascript.Areas.BackBone.Controllers.HomeController
LearnJavascript.Controllers.HomeController

Here is backbone route(This came with scaffolding, I did not make any changes):

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

Default route (This came with scaffolding, I did not make any changes):

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

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

I was thinking that unless the url starts with

http://localhost:46870/Backbone

the backbone area home will not be called. So, why is routing confused on this.

Most confusing part was when I called this url :

http://localhost:46870/home/index

It shows me the same error message. Why is MVC routing so much confused on this.

Iam using VS2013 and MVC5.


Solution

  • I got the correct answer from HamidBahmanabady.

    I added the namespace to the global route and it started working fine.

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "Test.Controllers"}
    

    and

            context.MapRoute(
                "BackBone_default",
                "BackBone/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional },
                new[] { "Test.Areas.Backbone.Controllers" }