使用区域,它似乎无法确定我想要的控制器

时间:2013-09-13 20:14:31

标签: c# asp.net-mvc routes asp.net-mvc-areas

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

/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 

'namespaces'参数。

我发现为什么它不起作用令人困惑,难道不能弄清楚这个UserController是不在Area中的那个吗?

2 个答案:

答案 0 :(得分:2)

当引入区域时,同名的控制器之间可能存在歧义参考: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
  );
}

答案 1 :(得分: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" }
);