自定义路由的ASP.NET MVC 3 URL路由问题

时间:2011-03-18 17:37:16

标签: asp.net-mvc asp.net-mvc-3 url-routing

我有以下自定义网址路由规则:

 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" })

我基本上希望网址看起来像“/ people / create / 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")那么它可以工作,但我没有参数。

我错过了什么?

由于

1 个答案:

答案 0 :(得分:2)

我想你想要这个方法overload。最后添加一个null:

@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
)
相关问题