【发布时间】:2021-04-06 04:16:04
【问题描述】:
所以我正在使用 MVC 5,并收到此错误。这是因为我有两个区域,它们都有一个同名和同名的控制器。
Multiple types were found that match the controller named 'controller1'.
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.
所以我知道我需要在路由中添加名称空间。所以在Route.config.cs我添加了这个
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "webApp.Controllers" }
);
然后在global.asax的application_start()函数中我添加了ControllerBuilder.Current.DefaultNamespaces.Add("webApp.Controllers");
然后在我的区域,我的错误来自哪里。我有 2 个区域,管理和 myportal。两者都有一个 AreaRegistration.cs 文件。在 AdminAreaRegistration.cs 我添加了这个
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional },
new[] { "webApp.Areas.Admin.Controllers" }
);
}
在 MyPortalAreaRegistration.cs 我添加了这个
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyPortal_default",
"MyPortal/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "webApp.Areas.MyPortal.Controllers" }
);
}
我在客户端打个电话
const otherParams = {
headers: {
"content-type": "application/json; charset=UTF-8"
},
body: JSON.stringify(product),
method: 'POST'
};
console.log(otherParams);
const response = await fetch('/Item/GetDetailPageURL', otherParams)
.then(response => {
response.json();
console.log(response);
window.open(response.url, "_blank");
})
.then(data => console.log(data));
据我了解,这就是让它工作所需的全部内容,但它仍在抱怨命名空间。我是不是误会/遗漏了什么?
【问题讨论】:
标签: c# asp.net-mvc routes