1

So I am using MVC 5, and is getting this error. This is because I have two areas and they both have a controller with the same name and same method name.

Multiple types were found that match the controller named 'controller1'. 
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.

So I understand that I need to add name spaces to my routing. So in Route.config.cs I added this

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

Then in the application_start() function in global.asax I added ControllerBuilder.Current.DefaultNamespaces.Add("webApp.Controllers");

Then in my areas, where my error is coming from. I have 2 areas, admin and myportal. Both have a AreaRegistration.cs file. In AdminAreaRegistration.cs I have this added

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
           defaults: new { action = "Index", id = UrlParameter.Optional },
           new[] { "webApp.Areas.Admin.Controllers" }
        );
    }

In MyPortalAreaRegistration.cs I have this added

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

From the client side I am making a post call

 const otherParams = {
        headers: {
            "content-type": "application/json; charset=UTF-8"
        },
        body: JSON.stringify(product),
        method: 'POST'
    };
    console.log(otherParams);
    const response = await fetch('/Item/GetDetailPageURL', otherParams)
        .then(response => {
            response.json();
            console.log(response);
            window.open(response.url, "_blank");
        })
        .then(data => console.log(data));

From what I understand this is all I needed to get it work, but it is still complaining about the namespace. Am I misunderstanding/ missing something?

Jack Thor
  • 1,554
  • 4
  • 24
  • 53

3 Answers3

3

You shouldn't need to change your RouteConfig.cs

All you need to do is to register your area (inside the area folder):

In this example, the area name is admin. You need to write your area registration code inside adminAreaRegistration.cs file which is inside admin folder:

enter image description here

public class adminAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "admin";
        }
    }

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

And then add AreaRegistration.RegisterAllAreas(); to your Global.asax.cs

Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
0

Besides area registration like Hooman Bahreini said, try to setup your default name spaces as well in the application start.

  1. Try this
protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);     
        // in you application start register your default or whatever makes sense Portal Controllers     
  ControllerBuilder.Current.DefaultNamespaces.Add("webApp.Areas.MyPortal.Controllers"); 
    }
  1. TRUST ME ON THIS -- go to your bin folder and blow away any old dll's "bin" folder completely manually. Sometimes there are stale dll's and don't get cleaned

  2. your routes seem fine, but double check your routes with this answer on routes...

Transformer
  • 6,963
  • 2
  • 26
  • 52
0

Use Attribute Routing in ASP.NET MVC 5:

[RoutePrefix(“reviews”)]
public class ReviewsController : Controller
{ 

Somewhere else:

[RoutePrefix(“otherreviews”)]
public class ReviewsController : Controller
{ 
tymtam
  • 31,798
  • 8
  • 86
  • 126