如何访问asp.net mvc中的查询string参数?
我想对我的视图应用不同的sorting和过滤我想通过查询string传递sorting和过滤参数 :
@Html.ActionLink("Name", "Index", new { SortBy= "Name"}) 这个简单的结构允许我sorting。 查看返回与查询string:
 ?SortBy=Name 
现在我想添加过滤,我想我的查询string结束
 ?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 
我想当用户按下sorting链接后,他应用一些过滤 – 过滤不会丢失,所以我需要一种方法来结合我的参数。 我的猜测是应该有一种方法不parsing查询string,但从一些MVC对象获取参数的集合。
 到目前为止,我最好的办法是创build一个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)%> 
 我的解决scheme与qwerty1000的类似。 我创build了一个扩展方法ActionQueryLink ,它采用与标准ActionLink相同的基本参数。 它通过Request.QueryString循环,并将find的所有参数添加到RouteValues字典中,但这些参数尚不存在(因此,如果需要,我们可以覆盖原始查询string)。 
要保留现有的string,但不添加任何键,用法将是:
 <%= Html.ActionQueryLink("Click Me!","SomeAction") %> 
要保留现有的string并添加新的密钥,用户将是:
 <%= 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); } 
 <%= Html.ActionLink("Name", "Index", new { SortBy= "Name", Filter="Something"}) %> 
为了保留查询string,您可以:
 <%= Html.ActionLink("Name", "Index", String.IsNullOrEmpty(Request.QueryString["SortBy"]) ? new { Filter = "Something" } : new { SortBy=Request.QueryString["SortBy"], Filter="Something"}) %> 
 或者如果你有更多的参数,你可以通过使用Request.QueryString手动build立链接。 
 使用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); } 
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" }) 
querystring会像:
 yourDomainRout/action/5?name=textName&abc=abc 
 它会有class="cssClass"