2011-05-25 32 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 { })