Routing Issue in Asp.Net Mvc

I have a list of puzzles that are tagged with specific β€œthemes”. Think about stackoverflow related issues with tags of specific categories.

I am trying to configure a route so that it works as follows:

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

My routes are configured as follows:

    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 is as follows:

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

My invocation of the action link in the views is as follows:

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

However, the problem I encountered is this:

The generated links appear as follows:

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

, "" null. querystring . ,

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

. ?

!

+3
3

actionName ( ) Html.ActionLink , "ThemedPuzzles".

, :

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

( ), ( ):

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

:

<li><%=Html.ActionLink(theme, "themed", new {controller = "puzzles", **action="ThemedPuzzles"** themes = theme})%></li>

, , URL {action}. ,

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

URL- ? , . , Routing , URL-, , .

.

0

.

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

If you have default values ​​for the sort type, page number, and page size, you can even set them here so that if they are not included, the default is passed.

0
source

Source: https://habr.com/ru/post/1715443/


All Articles