Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 2 Preview 2: Areas duplicate controller problem

I am continuing to enslave the MVC 2 thing: Areas...

Now I have two controllers with the same name (HomeController) in the main Controllers folder and in one of the Areas. Both have different namespaces so... theoretically should coexists, but they don't. The error is:

The controller name 'Home' is ambiguous between the following types:

Namespace.HomeController

Namespace.Areas.AreaName.Controllers.HomeController

This is not related to Home controller only (special one?), but applies to any pair in any areas.

How to achieve the coexistence of the same-name-controllers within different areas?

Thanks for your time :)

EDIT: It is okay for same controller name WITHIN different areas: registering routing with namespace solves the problem (thanks to Scott's Allen article ).

like image 879
twk Avatar asked Nov 03 '09 21:11

twk


2 Answers

If you create application namespace is MvcApplication1, you wrote try this.

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

  AreaRegistration.RegisterAllAreas();
  routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
    null,
    new[] { "MvcApplication1.Controllers" }
  );

}

Set root route controller namespace "MvcApplication1.Controllers", it running.

Hope this tips.

like image 57
takepara Avatar answered Oct 20 '22 00:10

takepara


If the two controllers with the same class name are in two different areas, this works as expected.

In your case, you have one controller in an area, and one controller in the "default Controllers folder". Are you sure this is what you want? Is your "default Controllers folder" supposed to contain some kind of shared controllers, such as the default account controller?

This is really an ASP.NET Routing issue as opposed to a namespace or class name issue. The problem is most likely that you have two routes to the ambiguous controller name; one registered via area registration and one via the default route registration in RegisterRoutes.

Also, see this post about area ordering.

like image 39
bzlm Avatar answered Oct 19 '22 23:10

bzlm