我继续奴役 MVC 2 东西:区域...
现在我在主 Controller 文件夹和其中一个区域中有两个同名的 Controller (HomeController)。两者都有不同的命名空间,所以......理论上应该共存,但他们没有。
错误是:
The controller name 'Home' is ambiguous between the following types:
Namespace.HomeController
Namespace.Areas.AreaName.Controllers.HomeController
这不仅仅与 Home Controller 有关(特殊的?),而是适用于任何领域的任何一对。
如何实现不同区域内的同名 Controller 共存?
谢谢你的时间 :)
编辑:
在不同区域内使用相同的 Controller 名称是可以的:使用命名空间注册路由解决了这个问题(感谢 Scott 的 Allen article)。
最佳答案
如果你创建的应用程序命名空间是 MvcApplication1,你写试试这个。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
AreaRegistration.RegisterAllAreas();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
null,
new[] { "MvcApplication1.Controllers" }
);
}
设置根路由 Controller 命名空间“MvcApplication1.Controllers”,它运行。
希望这个提示。
关于asp.net-mvc - ASP.NET MVC 2 预览 2 : Areas duplicate controller problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1670371/