使用区域,它似乎无法弄清楚我想要哪个控制器
本文关键字:弄清楚 我想要 控制器 区域 | 更新日期: 2023-09-27 18:31:19
我很困惑,我创建了一个名为"管理员"的区域,我有以下 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
"命名空间"参数。
我觉得为什么它不起作用很困惑,它不能弄清楚这个用户控制器是不在该区域的那个吗?
引入区域时,同名控制器之间可能存在歧义 参考: 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
);
}
您可以通过
将命名空间设置为路由来解决此问题
像下面的这个例子
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" }
);