2011-03-18 156 views
2

我有以下的自定义URL路由规则:ASP.NET MVC 3 URL路径问题定义路由

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

     routes.MapRoute(
      "RaceRoute", // Route name 
      "people/create/{raceid}/{id}", // URL with parameters 
      new { controller = "People", action = "Create", raceid = UrlParameter.Optional, id = UrlParameter.Optional } 
     ); 

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

我敢尝试与ActionLink的

使用
@Html.ActionLink("Add Person", "Create", "People", new { raceid = item.RaceId, id="1" }) 

我基本上希望URL看起来像 “/人/制作/ 5/1”

但生成的HTML看起来像

<a href="/races/Create?Length=6" id="1" raceid="5">Add Person</a> 

应该说<a href="/people/Create/5/1">Add Person</a>

我的网页是http://localhost:57355/races

如果我这样做只是@Html.ActionLink("Add Person", "Create", "People")那么它的工作原理,但我没有得到任何参数。

我错过了什么?

感谢

回答

2

我想你想这overload方法。在末尾添加一个空:

@Html.ActionLink("Add Person", "Create", "People", new { raceid = item.RaceId, id="1" }, null) 

这里是过载:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    RouteValueDictionary routeValues, 
    IDictionary<string, Object> htmlAttributes 
) 

抑或是这一个:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    Object routeValues, 
    Object htmlAttributes 
) 
+0

非常感谢您的帮助。 – Mike 2011-03-18 18:07:53