2011-05-25 30 views
6

我正在使用MVC3并在我的应用程序中有区域。在一般情况下,一切工作正常,我可以浏览到我的区域(例如管理员=面积,控制器=部门)是这样的:带区域的MVC - Html.ActionLink返回错误的URL /路由?

<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null)%> 

不过,我注意到的是,如果我不指定地区我ActionLink,例如

<%: Html.ActionLink("Test", "Index", "Home")%> 

如果我导航到“管理”区域,这将停止工作。即我的网址是现在 http://localhost/myproj/Admin/Home/Index 而不是 http://localhost/myproj/Home/Index

上是怎么回事任何想法?

我的路线是创建MVC应用程序/区域时的所有默认值。即

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[] { "MyProj.Controllers" } 
     ); 
    } 

而我区登记

public override void RegisterArea(AreaRegistrationContext context) 
    { 
     context.MapRoute(
      "Admin_default", 
      "Admin/{controller}/{action}/{id}", 
      new { action = "Index", id = UrlParameter.Optional } 
     ); 
    } 

设计是这样吗? 此公告建议使用RouteLink代替的ActionLink的: 说使用RouteLink Is it required to have "area" routevalue on actionlink if the application was grouped into areas?

+1

这是设计吗?也许你必须明确指定Areas =“”? – Raymond 2011-05-25 23:47:41

+0

可能的重复[是否需要在actionlink上有“area”routevalue,如果应用程序被分组为区域?](http://stackoverflow.com/questions/5549497/is-it-required-to-have-area- routevalue-ON-ActionLink的-IF-的应用程序,是-GR) – 2011-11-03 08:27:22

回答

7

This StackOverflow answer正确地指出,你需要,如果你正在使用等方面,提供Area名。

例如。

Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })