【问题标题】:Routing issue in Asp.Net MvcAsp.Net Mvc 中的路由问题
【发布时间】:2009-08-18 16:29:11
【问题描述】:

我有一个带有特定“主题”标签的谜题列表。思考关于带有特定类别标签的 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
        );

我的控制器如下所示:

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

视图中我的操作链接调用如下所示:

        <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

要添加到这个问题,控制器操作上的“主题”参数始终为空。它从不将查询字符串参数转换为控制器操作参数。但是,如果我手动导航到

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

一切正常。我错过了什么?

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    您对Html.ActionLink 的调用的actionName 参数(第二个参数)与您在"ThemedPuzzles" 路由中指定的操作不匹配。

    很像Phil的建议,试试:

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

    或者您可以直接调用路由(因为它已命名),而无需指定控制器或操作(因为它们将从路由默认值中获取):

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

    【讨论】:

      【解决方案2】:

      试试这个:

      <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 在 defaults 字典中看到一个参数不是实际 URL 中的参数时,它要求您指定该值以区分这两个路由。

      在这种情况下,它需要您指定动作来区分路由。

      【讨论】:

      • 那还是不行。我应该这样做与我尝试的方式完全不同吗?
      【解决方案3】:

      在你的路由声明中,尝试在你的控制器方法中指定每个输入参数。

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

      如果您有排序类型、页码和页面大小的默认值,您甚至可以在此处设置它们,如果不包含它们,则传入默认值。

      【讨论】:

      • 没有必要在你的路由声明中提供每个输入参数——因为 sortType、page 和 pageSize 参数都是可以为空的.
      • 你是对的,但以我个人的经验,如果你希望 URL 位于友好的文件夹结构中而不是使用查询字符串,则需要定义所有部分。我一直都是这样解决的。
      猜你喜欢
      • 2013-11-13
      • 2011-11-30
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多