1

我一直在尋找解決方案來解決我的問題。發現了很多類似的問題,但沒有一個能爲我提供解決方案。ASP.Net MVC 2區域,子區域和路線

我想在一個區域內註冊一個區域。然而,這種方法可以「部分地」把我的路由搞砸了。在他們註冊的順序,考慮FooBar的和Foo註冊

我的路線登記從AreaRegistrations

現身
routes.MapRoute("FooBar_default", 
      "Foo/Bar/{controller}/{action}", 
      new { area = "Foo/Bar", controller = "Home", action = "Index"}, 
      new[] { BarHomeControllerType.Namespace } 
); 

    routes.MapRoute("Foo_default", 
      "Foo/{controller}/{action}/{id}", 
      new { area = "Foo", controller = "Start", action = "Index", id = UrlParameter.Optional }, 
      new { controller = new NotSubArea()}, 
      new[] { typeof(StartController).Namespace } 
     ); 

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute("PagesRoute", "Pages/{action}", new { controller = "Pages", Action "Index" }).DataTokens["UseNamespaceFallback"] = false; 

    routes.MapRoute("Default", // Route name 
      "{controller}/{action}/{id}", 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      new[] { typeof(HomeController).Namespace } 
      ).DataTokens["UseNamespaceFallback"] = false; 

現在出現以下問題。當去網站/美孚/或網站/美孚/酒吧在這些頁面上的鏈接生成正確使用:

!{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = "Foo/Bar"})} 
    or 
    !{ Url.Action("Index", "Home", new { area = "Foo/Bar"}) } //or a different area 

但是當我使用這在我的主要頁面,換句話說網站/或網站/主頁等。

!{Html.ActionLink<HomeController>(c => c.Index(),"Home", new { area = ""})} 
    or 
    !{ Url.Action("Index", "Home", new { area = ""}) } 
    //or with no area identifier specified 

它生成Url:Website/Foo/Bar/Home等......哪一個是錯的。

當我刪除Foo/Bar的區域註冊時,它全部重新運行。直接去到網站/首頁/關於或網站/首頁顯示正確的頁面,所以即時猜測內部UrlHelper挑選錯誤的路線來呈現。

我嘗試切換FooBar_default和Foo_Default路由的順序,以便Foo_default路由在FooBar_default路由之前註冊,但是此區域不再工作(找不到資源)並且鏈接仍然生成不正確。

我覺得最奇怪的是,刪除Foo/Bar註冊可以解決問題。我希望有人可以在這個問題上提出一些見解..

+0

將註冊更改爲Foo-Bar而不是使用/沒有區別.. – Danthar 2010-07-19 10:17:08

回答

1

你需要了解的是,Area只是一個路由概念,微軟已經整齊地包裝了概念或UrlRouting讓人們開始。

實際上,您可以根據自己的需求獲取MVC框架來路由您的請求,然而您喜歡。

你可能需要看看幹什麼,寫你自己的RouteHandler。這將使您能夠正確地指導MVC框架如何根據您的要求路由任何請求。

請參閱this answerasp.net mvc complex routing for tree path作爲一個例子,讓你開始。

chris166概述了我實現自己的IRouteHandler,並映射你的路線來使用它,而應該得到你所需要的。它比使用盒子解決方案更加努力,但應該讓你獲得更好的結果。

routes.MapRoute(
    "Tree", 
    "Tree/{*path}", 
    new { controller = "Tree", action = "Index" }) 
      .RouteHandler = new TreeRouteHandler();