문제

I'm confused, I created an area named 'Admin' and I have these 2 controllers:

/admin/users/...

and

/users/..

Now if I try and link to this url:

/users/list

I get this error:

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

I find it confusing why it doesn't work, can't it figure out that this UserController is the one that isn't in the Area?

도움이 되었습니까?

해결책

When areas are introduced, there is the potential for ambiguity between identically named Controllers ref: http://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx

Try adding this in your Global.asax

public static void RegisterRoutes(RouteCollection routes)
{
  //all your other routes

  routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL
    new { controller = "Home", action = "Index", id = "" }, // Defaults
    new[]{"Your.NameSpace"}                       // Namespaces
  );
}

다른 팁

you can solve this problem by setting the namespace to your routes

like this sample below

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] { "My.Controllers" }
);

context.MapRoute(
    "Admin_default",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    new[] { "My.Areas.Admin.Controllers" }
);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top