我尝试在我的项目中实现 Phil 的 Areas Demo
http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx .
我在现有的 MVC 项目中附加了 Areas/Blog 结构,但在我的项目中出现以下错误。
Controller 名称 Home
在以下类型之间不明确:
WebMVC.Controllers.HomeController
WebMVC.Areas.Blogs.Controllers.HomeController
这就是我的 Global.asax
的外观。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapAreas("{controller}/{action}/{id}",
"WebMVC.Areas.Blogs",
new[] { "Blogs", "Forums" });
routes.MapRootArea("{controller}/{action}/{id}",
"WebMVC",
new { controller = "Home", action = "Index", id = "" });
//routes.MapRoute(
// "Default", // Route name
// "{controller}/{action}/{id}",// URL with parameters
// new { controller = "Home", action = "Index", id = "" }
// // Parameter defaults
//);
}
protected void Application_Start()
{
String assemblyName = Assembly.GetExecutingAssembly().CodeBase;
String path = new Uri(assemblyName).LocalPath;
Directory.SetCurrentDirectory(Path.GetDirectoryName(path));
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new AreaViewEngine());
RegisterRoutes(RouteTable.Routes);
// RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}
如果我从 routes.MapAreas
中删除 /Areas/Blogs
,它会查看根的索引。
最佳答案
在 ASP.NET MVC 2.0 中,您可以在父区域中注册路由时包含父项目 Controller 的命名空间。
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" },
new string[] { "MyProjectName.Controllers" }
);
这将路由限制为仅在您指定的命名空间中搜索 Controller 。
关于asp.net-mvc - ASP.NET MVC 中不明确的 Controller 名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/451831/