3

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.

9
  • even in different areas you can't have 2 controllers with the same name. Commented Jul 29, 2014 at 18:43
  • I thought having the same controller names was the main purpose of having an area. Commented Jul 29, 2014 at 18:44
  • 1
    Please check [Multiple types were found that match the controller named 'Home'][1] [1]: stackoverflow.com/questions/7842293/… Commented Jul 29, 2014 at 18:52
  • @HamidBahmanabady I added the namespace to the global route and it started working fine. thanks. Commented Jul 29, 2014 at 18:59
  • @MattBodily We can have the same controller name in areas. That is one of the main purpose of having areas. I just created another area and double checked it. :) Commented Jul 29, 2014 at 19:00

2 Answers 2

5

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" }
Sign up to request clarification or add additional context in comments.

1 Comment

hi, I've tried yours code, but it comes an error again The request for 'Health' has found the following matching controllers: Appname.Areas.Admin.Controllers.HealthController Appname.Controllers.HealthController, What should I do?
1

try this,

In route.config

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

and Admin section --> AdminAreaRegistration.cs

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.