asp.net-mvc 如何在 asp.net mvc 中访问查询字符串参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/594019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I access query string parameters in asp.net mvc?
提问by Alexander Taran
I want to have different sorting and filtering applied on my view I figured that I'll be passing sorting and filtering paramsthrough query string:
我想在我的视图上应用不同的排序和过滤我想我将通过查询字符串传递排序和过滤参数:
@Html.ActionLink("Name", "Index", new { SortBy= "Name"})
This simple construction allows me to sort. View comes back with this in query string:
这个简单的结构让我可以排序。视图在查询字符串中返回:
?SortBy=Name
Now I want to add filtering and i want my query string to end up with
现在我想添加过滤,我希望我的查询字符串以
?SortBy=Name&Filter=Something
How can I add another parameter to list of already existing ones in ActionLink? for Example:
如何将另一个参数添加到ActionLink. 例如:
user requests /Index/
view has
视图有
@Html.ActionLink("Name", "Index", new { SortBy= "Name"})
and
和
@Html.ActionLink("Name", "Index", new { FilterBy= "Name"})
Links: The first one looks like /Index/?SortBy=Nameand The second is /Index/?FilterBy=Name
链接:第一个看起来像/Index/?SortBy=Name,第二个是 /Index/?FilterBy=Name
I want when user pressed sorting link after he applied some filtering - filtering is not lost, so i need a way to combine my params. My guess is there should be a way to not parse query string, but get collection of parameters from some MVC object.
我希望当用户在应用一些过滤后按下排序链接时 - 过滤不会丢失,所以我需要一种方法来组合我的参数。我的猜测是应该有一种方法可以不解析查询字符串,而是从某个 MVC 对象中获取参数集合。
采纳答案by Alexander Taran
so far the best way I figured out is to create a copy of ViewContext.RouteData.Valuesand inject QueryString values into it.
and then modify it before every ActionLinkusage.
still trying to figure out how to use .Union()instead of modifying a dictionary all the time.
到目前为止,我想出的最好方法是创建一个副本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)%>
回答by Brian Cauthon
My solution is similar to qwerty1000's. I created an extension method, ActionQueryLink, that takes in the same basic parameters as the standard ActionLink. It loops through Request.QueryString and adds any parameters found to the RouteValuesdictionary that are not already present (so we can overwrite the original query string if needed).
我的解决方案类似于 qwerty1000。我创建了一个扩展方法 ,ActionQueryLink它采用与标准ActionLink. 它循环遍历 Request.QueryString 并将找到的任何参数添加到RouteValues字典中尚不存在(因此我们可以在需要时覆盖原始查询字符串)。
To preserve the existing string but not add any keys the usage would be:
要保留现有字符串但不添加任何键,用法将是:
<%= Html.ActionQueryLink("Click Me!","SomeAction") %>
To preserve the existing string and add new keys the user would be:
要保留现有字符串并添加新键,用户将是:
<%= Html.ActionQueryLink("Click Me!","SomeAction", new{Param1="value1", Param2="value2"} %>
The code below is for the two usages, but it should be pretty easy to add other overloads to match the other ActionLinkextensions as needed.
下面的代码适用于这两种用法,但添加其他重载以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);
}
回答by Mehrdad Afshari
<%= Html.ActionLink("Name", "Index", new { SortBy= "Name", Filter="Something"}) %>
To preserve the querystring you can:
要保留查询字符串,您可以:
<%= Html.ActionLink("Name", "Index",
String.IsNullOrEmpty(Request.QueryString["SortBy"]) ?
new { Filter = "Something" } :
new { SortBy=Request.QueryString["SortBy"], Filter="Something"}) %>
Or if you have more parameters, you could build the link manually by using taking Request.QueryStringinto account.
或者,如果您有更多参数,您可以使用考虑来手动构建链接Request.QueryString。
回答by user170868
Use ActionLinkCombinedinstead of ActionLink
使用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);
}
回答by syed Ahsan Jaffri
MVC4
MVC4
@Html.ActionLink("link text","action",new { @id = 5, @name = "textName", @abc = "abc" })
OR
或者
@Html.ActionLink("link text", "action", "controller", new { @id = 5, @name = "textName", @abc = "abc" }, new { @class = "cssClass" })
querystring would be like:
查询字符串会是这样的:
yourDomainRout/action/5?name=textName&abc=abc
it would have class="cssClass"
它会有 class="cssClass"

