gpt4 book ai didi

c# - 发现多个类型与名为 'Home' 的 Controller 匹配 - 在两个不同的区域

转载 作者:可可西里 更新时间:2023-11-01 07:52:38 27 4
gpt4 key购买 nike

我的项目有两个区域。现在,当我运行该程序时,出现此错误:

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

The request for 'Home' has found the following matching controllers:
BaseAdminMVC.Areas.BaseAdmin.Controllers.HomeController
BaseAdminMVC.Areas.TitomsAdmin.Controllers.HomeController

我在这里找到了一些来源:Multiple Controller name
但我认为它只适用于一个领域。
就我而言,我在不同领域有两个项目。希望有人能告诉我应该怎么做才能解决这个问题。
这是 Global.asax 文件:

public static void RegisterRoutes(RouteCollection routes)
{
string[] namespaces = new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers", "BaseAdminMVC.Areas.TitomsAdmin.Controllers"};

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces
);
}

顺便说一下,我在 Area 文件夹外还有一个 Controller (“HomeController”)。这仅提供了两个项目 BaseAdminTitomsAdmin 的链接。

我已经尝试过这个解决方案,但仍然不起作用:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
"BaseAdmin",
"BaseAdmin/{controller}/{action}",
new { controller = "Account", action = "Index" },
new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers" }
);

routes.MapRoute(
"TitomsAdmin",
"TitomsAdmin/{controller}/{action}",
new { controller = "Home", action = "Index" },
new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
);

提前致谢!

最佳答案

我不知道发生了什么,但这段代码工作正常:

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
);
}

关于c# - 发现多个类型与名为 'Home' 的 Controller 匹配 - 在两个不同的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8788014/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com