Routing issue in Asp.Net Mvc

asp.net-mvcasp.net-mvc-routing

I have a list of puzzles that are tagged with specific "Themes". Think questions on stackoverflow tagged with certain categories.

I'm trying to get my route setup so that it works like this:

http://www.wikipediamaze.com/puzzles/themed/Movies
http://www.wikipediamaze.com/puzzles/themed/Movies,Another-Theme,And-Yet-Another-One

My routes are setup like this:

    routes.MapRoute(
        "wiki",
        "wiki/{topic}",
        new {controller = "game", action = "continue", topic = ""}
        );

    routes.MapRoute(
        "UserDisplay",
        "{controller}/{id}/{userName}",
        new {controller = "users", action = "display", userName=""},
        new { id = @"\d+" }
        );

    routes.MapRoute(
        "ThemedPuzzles",
        "puzzles/themed/{themes}",
        new { controller = "puzzles", action = "ThemedPuzzles", themes = "" }
        );

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

My Controller looks like this:

public ActionResult ThemedPuzzles(string themes, PuzzleSortType? sortType, int? page, int? pageSize)
{
    //Logic goes here
}

My Action link call in the views looks like this:

        <ul>
        <%foreach (var theme in Model.Themes)
          { %>
            <li><%=Html.ActionLink(theme, "themed", new {controller = "puzzles", themes = theme})%></li>
            <% } %>
        </ul>

However the problem I'm having is this:

The links that get generated show up like this:

http://www.wikipediamaze.com/puzzles/themed?themes=MyThemeNameHere

To add to this problem the "Themes" parameter on the controller action always comes through as null. It never translates the querystring parameter to the controller action parameter. However, if I manually navigate to

http://www.wikipediamaze.com/puzzles/themed/MyThemeNameHere
http://www.wikipediamaze.com/puzzles/themed/MyThemeNameHere,Another-ThemeName

everything works just fine. What am I missing?

Thanks in advance!

Best Answer

The actionName parameter (second parameter) of your call to Html.ActionLink doesn't match the action you've specified in your "ThemedPuzzles" route.

Very similar to Phil's suggestion, try:

<%= Html.ActionLink(theme, "ThemedPuzzles", new { controller = "puzzles", themes = theme }) %>

Or you can call to the route directly (since it's named) without needing to specify the controller or action (as they'll be picked up from the route defaults):

<%= Html.RouteLink(theme, "ThemedPuzzles", new { themes = theme }) %>