R – ASP.NET MVC routing

asp.net-mvcrouting

Up until now I've been able to get away with using the default routing that came with ASP.NET MVC. Unfortunately, now that I'm branching out into more complex routes, I'm struggling to wrap my head around how to get this to work.

A simple example I'm trying to get is to have the path /User/{UserID}/Items to map to the User controller's Items function. Can anyone tell me what I'm doing wrong with my routing here?

routes.MapRoute("UserItems", "User/{UserID}/Items", 
                      new {controller = "User", action = "Items"});

And on my aspx page

Html.ActionLink("Items", "UserItems", new { UserID = 1 })

Best Answer

Going by the MVC Preview 4 code I have in front of me the overload for Html.ActionLink() you are using is this one:

public string ActionLink(string linkText, string actionName, object values);

Note how the second parameter is the actionName not the routeName.

As such, try:

Html.ActionLink("Items", "Items", new { UserID = 1 })

Alternatively, try:

<a href="<%=Url.RouteUrl("UserItems", new { UserId = 1 })%>">Items</a>