2010-09-03 96 views
0

我已經定義了一系列路由在Global.asax.cs中:這些asp.net mvc路由規則有什麼問題?

public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
     routes.MapRoute(null, "", // Only matches the empty URL (i.e. ~/) 
         new 
         { 
          controller = "Stream", 
          action = "Entry", 
          streamUrl = "Pages", 
          entryUrl = "HomePage" 
         } 
     ); 

     routes.MapRoute(null, "{streamUrl}", // matches ~/Pages 
         new { controller = "Stream", action = "List" } 
     ); 

     routes.MapRoute(null, "{streamUrl}/{entryUrl}", // matches ~/Pages/HomePage 
         new { controller = "Stream", action = "Entry" } 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

當我把在mydomain.com/或mydomain.com/Pages/HomePage路線工程完全按照我的期望。所以現在我正在編寫一個局部視圖來生成一個鏈接列表。作爲一個測試,我把這個代碼在局部視圖:

<ul> 
<% foreach (var item in Model) { %> 

     <li id="<%:item.Text.Replace(" ", "-") %>"> 

      //This works - link shows up in browser as mydomain.com/ 
      <%: Html.RouteLink(item.Text, new{ streamUrl = "Pages", entryUrl = "HomePage" }) %> 

      //This does not work - link shows up as mydomain.com/Blog?entryUrl=BlogEntryOne 
      //instead of mydomain.com/Blog/BlogEntryOne 
      <%: Html.RouteLink(item.Text, new{ streamUrl = "Blog", entryUrl = "BlogEntryOne" }) %> 

     </li> 

<% } %> 
</ul> 

我不知道爲什麼entryUrl路線值沒有被正確註冊。我錯過了什麼?

回答

2

我不非常習慣於用MVC,但我覺得你應該首先把你的更具體的航線,如:

routes.MapRoute(null, "{streamUrl}/{entryUrl}", // matches ~/Pages/HomePage 
       new { controller = "Stream", action = "Entry" } 
); 

routes.MapRoute(null, "{streamUrl}", // matches ~/Pages 
       new { controller = "Stream", action = "List" } 
); 
+0

哦,天啊......我無法看透的森林樹木。謝謝! – quakkels 2010-09-03 21:07:11