asp.net-mvc - ASP.NET MVC 中不明确的 Controller 名称

标签 asp.net-mvc

我尝试在我的项目中实现 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/

相关文章:

html - Kendo 不显示任何数据它还在 mvc 中显示空白行

asp.net-mvc - 对普通 Controller 使用 JSON.NET 格式化程序

asp.net-mvc - 遵循最佳编程实践的 Asp.Net Mvc 应用程序所需/推荐的所有层的分割是什么?

javascript - 如何使用 jQuery 验证插件验证美国邮政编码

javascript - 在 VS 2013 的 MVC 中使用 Foundation Zurb 时样式不起作用

asp.net-mvc - 像 .NET 中的 moment.js 一样格式化日期

c# - MVC 3 将模型项传递给@Html.ActionLink 文本?

asp.net - 将旧帖子重定向到 ServiceStack API

c# - 你调用的对象是空的

c# - @Html.EditorFor DateTime 在为其设置默认值时不显示