0

我很困惑,我创建了一个名为“Admin”的区域,我有这 2 个控制器:

/admin/users/...

/users/..

现在,如果我尝试链接到此网址:

/users/list

我收到此错误:

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 

“命名空间”参数。

我觉得它为什么不起作用很令人困惑,难道它不能确定这个 UserController 是不在该区域中的那个吗?

4

2 回答 2

2

引入区域时,同名控制器之间可能会出现歧义 ref:http ://haacked.com/archive/2010/01/12/ambiguous-controller-names.aspx

尝试将其添加到您的 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
  );
}
于 2013-09-13T20:27:48.263 回答
0

您可以通过将命名空间设置为您的路线来解决此问题

像下面的这个样本

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" }
);
于 2013-09-13T20:19:37.933 回答