【问题标题】:How do I access query string parameters in asp.net mvc?如何访问 asp.net mvc 中的查询字符串参数?
【发布时间】:2010-10-10 06:58:25
【问题描述】:

我想在我的视图上应用不同的排序和过滤 我想我将通过查询字符串传递排序和过滤 params:

@Html.ActionLink("Name", "Index", new { SortBy= "Name"})

这个简单的结构让我可以进行排序。 View 在查询字符串中返回这个:

?SortBy=Name

现在我想添加过滤,我希望我的查询字符串以

结尾
?SortBy=Name&Filter=Something

如何在ActionLink 中的现有参数列表中添加另一个参数?例如:

user requests /Index/

视图有

 @Html.ActionLink("Name", "Index", new { SortBy= "Name"})

和

 @Html.ActionLink("Name", "Index", new { FilterBy= "Name"})

链接:第一个看起来像/Index/?SortBy=Name,第二个是/Index/?FilterBy=Name

我希望用户在应用一些过滤后按下排序链接 - 过滤不会丢失,所以我需要一种方法来组合我的参数。 我的猜测是应该有一种方法不解析查询字符串,而是从一些 MVC 对象中获取参数集合。

【问题讨论】:

  • 对问题进行剃刀语法更新怎么样?
  • 你去吧.. 只花了我 4 年.. (-:

标签: asp.net-mvc query-string


【解决方案1】:
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name", Filter="Something"}) %>

要保留查询字符串,您可以:

<%= Html.ActionLink("Name", "Index", 
     String.IsNullOrEmpty(Request.QueryString["SortBy"]) ? 
        new { Filter = "Something" } : 
        new { SortBy=Request.QueryString["SortBy"], Filter="Something"}) %>

或者如果您有更多参数,您可以使用Request.QueryString 手动构建链接。

【讨论】:

    【解决方案2】:

    到目前为止,我想出的最好方法是创建ViewContext.RouteData.Values 的副本 并将 QueryString 值注入其中。 然后在每次使用 ActionLink 之前对其进行修改。 仍在试图弄清楚如何使用.Union() 而不是一直修改字典。

    <% RouteValueDictionary   tRVD = new RouteValueDictionary(ViewContext.RouteData.Values); %>
    
    <% foreach (string key in Request.QueryString.Keys )
        {
             tRVD[key]=Request.QueryString[key].ToString();
        } %>
    
    <%tRVD["SortBy"] = "Name"; %>
                    <%= Html.ActionLink("Name", "Index", tRVD)%>
    

    【讨论】:

    • 另一个question 表示这是使用@Html.ActionLink 时的最佳方式。我选择&lt;a href="Index?@Request.QueryString"&gt;Name&lt;/a&gt;。
    • 使用 ASP.NET MVC 3 或 4 有更好的方法吗?
    • @LenielMacaferi,查看模型
    【解决方案3】:

    使用ActionLinkCombined 而不是ActionLink

            public static string ActionLinkCombined(this HtmlHelper htmlHelper, string linkText, string actionName,
                                                object routeValues)
        {
            var dictionary = new RouteValueDictionary();
            foreach (var pair in htmlHelper.ViewContext.Controller.ValueProvider)
                dictionary[pair.Key] = pair.Value.AttemptedValue;
            if (routeValues != null)
            {
                foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(routeValues))
                {
                    object o = descriptor.GetValue(routeValues);
                    dictionary[descriptor.Name] = o;
                }
            }
            return htmlHelper.ActionLink(linkText, actionName, dictionary);
        }
    

    【讨论】:

      【解决方案4】:

      我的解决方案类似于 qwerty1000 的。我创建了一个扩展方法ActionQueryLink,它采用与标准ActionLink 相同的基本参数。它循环通过 Request.QueryString 并将找到的任何参数添加到 RouteValues 字典中尚不存在(因此我们可以在需要时覆盖原始查询字符串)。

      要保留现有字符串但不添加任何键,用法如下:

      <%= Html.ActionQueryLink("Click Me!","SomeAction") %>
      

      要保留现有字符串并添加新键,用户将是:

      <%= Html.ActionQueryLink("Click Me!","SomeAction", new{Param1="value1", Param2="value2"} %>
      

      下面的代码用于这两种用法,但根据需要添加其他重载以匹配其他 ActionLink 扩展应该很容易。

          public static string ActionQueryLink(this HtmlHelper htmlHelper,
              string linkText, string action)
          {
              return ActionQueryLink(htmlHelper, linkText, action, null);
          }
      
          public static string ActionQueryLink(this HtmlHelper htmlHelper, 
              string linkText, string action, object routeValues)
          {
              var queryString =
                  htmlHelper.ViewContext.HttpContext.Request.QueryString;
      
              var newRoute = routeValues == null 
                  ? htmlHelper.ViewContext.RouteData.Values 
                  : new RouteValueDictionary(routeValues);
      
              foreach (string key in queryString.Keys)
              {
                  if (!newRoute.ContainsKey(key)) 
                      newRoute.Add(key, queryString[key]);
              }
              return HtmlHelper.GenerateLink(htmlHelper.ViewContext.RequestContext,
                  htmlHelper.RouteCollection, linkText, null /* routeName */, 
                  action, null, newRoute, null);
          }
      

      【讨论】:

        【解决方案5】:

        MVC4

        @Html.ActionLink("link text","action",new { @id = 5, @name = "textName", @abc = "abc" })
        

        或

         @Html.ActionLink("link text", "action", "controller", new { @id = 5, @name = "textName", @abc = "abc" }, new { @class = "cssClass" })
        

        查询字符串会像:

        yourDomainRout/action/5?name=textName&abc=abc
        

        应该是class="cssClass"

        【讨论】:

        • 看到这一行new { @id = 5, @name = "textName", @abc = "abc" }为什么你多次使用@符号。 @ 符号用于保留关键字。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-22
        • 2013-02-08
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多