我有一个带有特定“主题”标签的谜题列表。思考 stackoverflow 上标记有特定类别的问题。
我正在尝试设置我的路线,使其像这样工作:
http://www.wikipediamaze.com/puzzles/themed/Movies http://www.wikipediamaze.com/puzzles/themed/Movies,Another-Theme,And-Yet-Another-One
我的路线设置如下:
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
);
我的 Controller 如下所示:
public ActionResult ThemedPuzzles(string themes, PuzzleSortType? sortType, int? page, int? pageSize)
{
//Logic goes here
}
View 中的“我的操作”链接调用如下所示:
<ul>
<%foreach (var theme in Model.Themes)
{ %>
<li><%=Html.ActionLink(theme, "themed", new {controller = "puzzles", themes = theme})%></li>
<% } %>
</ul>
但是我遇到的问题是:
生成的链接如下所示:
http://www.wikipediamaze.com/puzzles/themed?themes=MyThemeNameHere
为了解决此问题, Controller 操作上的“主题”参数始终为空。它永远不会将查询字符串参数转换为 Controller 操作参数。但是,如果我手动导航到
http://www.wikipediamaze.com/puzzles/themed/MyThemeNameHere http://www.wikipediamaze.com/puzzles/themed/MyThemeNameHere,Another-ThemeName
一切都很好。我错过了什么?
提前致谢!
最佳答案
调用 Html.ActionLink
的 actionName
参数(第二个参数)与您在 “ThemedPuzzles”中指定的操作不匹配
路线。
与 Phil 的建议非常相似,请尝试:
<%= Html.ActionLink(theme, "ThemedPuzzles", new { controller = "puzzles", themes = theme }) %>
或者您可以直接调用路由(因为它已命名),而无需指定 Controller 或操作(因为它们将从路由默认值中获取):
<%= Html.RouteLink(theme, "ThemedPuzzles", new { themes = theme }) %>
关于asp.net-mvc - Asp.Net Mvc 中的路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1295013/