asp.net-mvc - Asp.Net Mvc 中的路由问题

标签 asp.net-mvc asp.net-mvc-routing

我有一个带有特定“主题”标签的谜题列表。思考 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.ActionLinkactionName 参数(第二个参数)与您在 “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/

相关文章:

C#以更少的内存消耗从服务器下载大文件

asp.net-mvc - 登录后重定向以返回url

c# - ASP.NET 路由,其中​​两个 Controller 操作具有相同数量的参数

asp.net-mvc - ASP.NET MVC 4 - 2 个带段的区域,1 个不带段的区域

ASP.NET MVC 路由 : How to make my MVC URL's have . aspx 扩展名?

asp.net - System.Data.SqlClient.SqlException : The SELECT permission was denied on object 'tableName' , 数据库 'dbName' ,架构 'dbo'

c# - EditorHelper 是否使用 ViewBag 将数据传递给相应的模板?

asp.net-mvc - 如何使用 ASP.NET MVC 1.0 直接从文件系统提供文件?

asp.net-mvc - 在项目之间共享 URL 的优雅方式?

c# - MVC 部分 View 、GET 和路由