2010-09-03 52 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