开发者

ASP.NET MVC 3 URL Routing problem with Custom Route

开发者 https://www.devze.com 2023-02-18 05:21 出处:网络
I have the following Custom URL Routing Rule: routes.IgnoreRoute(\"{resource}.axd/{*pathInfo}\"); routes.MapRoute(

I have the following Custom URL Routing Rule:

 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
        );

Which I'm trying to use with Ac开发者_高级运维tionlink

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

I basically want the url to look like "/people/create/5/1"

But the HTML generated looks like

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

It should say <a href="/people/Create/5/1">Add Person</a>

The page I'm on is http://localhost:57355/races

If I do just @Html.ActionLink("Add Person", "Create", "People") then it works but I get no parameters.

What am I missing?

Thanks


I think you want this overload of the method. Add a null at the end:

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

Here is the overload:

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

or is it this one:

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

精彩评论

暂无评论...
验证码 换一张
取 消