如何在 C# 中更新查询字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9771718/
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 to update querystring in C#?
提问by developer747
Somewhere in the url there is a &sortBy=6 . How do I update this to &sortBy=4 or &sortBy=2 on a button click? Do I need to write custom string functions to create the correct redirect url?
在 url 的某个地方有一个 &sortBy=6 。如何在单击按钮时将其更新为 &sortBy=4 或 &sortBy=2?我是否需要编写自定义字符串函数来创建正确的重定向 url?
If I just need to append a query string variable I would do
如果我只需要附加一个查询字符串变量,我会这样做
string completeUrl = HttpContext.Current.Request.Url.AbsoluteUri + "&" + ...
Response.Redirect(completeUrl);
But what I want to do is modify an existing querystring variable.
但我想要做的是修改现有的查询字符串变量。
采纳答案by Ahmad Mageed
To modify an existing QueryString value use this approach:
要修改现有的 QueryString 值,请使用以下方法:
var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
nameValues.Set("sortBy", "4");
string url = Request.Url.AbsolutePath;
Response.Redirect(url + "?" + nameValues); // ToString() is called implicitly
I go into more detail in another response.
我在另一个回复中更详细地介绍。
回答by Stilgar
You need to redirect to a new URL. If you need to do some work on the server before redirecting there you need to use Response.Redirect(...) in your code. If you do not need to do work on the server just use HyperLink and render it in advance.
您需要重定向到一个新的 URL。如果您需要在重定向之前在服务器上做一些工作,您需要在代码中使用 Response.Redirect(...)。如果您不需要在服务器上进行工作,只需使用 HyperLink 并提前渲染即可。
If you are asking about constructing the actual URL I am not aware of any built-in functions that can do the job. You can use constants for your Paths and QueryString arguments to avoid repeating them all over your code.
如果您询问构建实际 URL,我不知道任何可以完成这项工作的内置函数。您可以为 Paths 和 QueryString 参数使用常量,以避免在整个代码中重复它们。
The UriBuildercan help you building the URL but not the query string
该UriBuilder可以帮助你建立网址而不是查询字符串
回答by Massimiliano Peluso
The only way you have to change the QueryStringis to redirect to the same page with the new QueryString:
您必须更改的唯一方法QueryString是使用新的 QueryString 重定向到同一页面:
Response.Redirect("YourPage.aspx?&sortBy=4")
http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.100).aspx
回答by Strillo
You can do it clientside with some javascript to build the query string and redirect the page using windows.open.
您可以在客户端使用一些 javascript 来构建查询字符串并使用windows.open重定向页面。
Otherwise you can use Response.Redirect or Server.Transfer on the server-side.
否则,您可以在服务器端使用 Response.Redirect 或 Server.Transfer。
回答by James Hull
SolrNet has some very helpful Url extension methods. http://code.google.com/p/solrnet/source/browse/trunk/SampleSolrApp/Helpers/UrlHelperExtensions.cs?r=512
SolrNet 有一些非常有用的 Url 扩展方法。http://code.google.com/p/solrnet/source/browse/trunk/SampleSolrApp/Helpers/UrlHelperExtensions.cs?r=512
/// <summary>
/// Sets/changes an url's query string parameter.
/// </summary>
/// <param name="helper"></param>
/// <param name="url">URL to process</param>
/// <param name="key">Query string parameter key to set/change</param>
/// <param name="value">Query string parameter value</param>
/// <returns>Resulting URL</returns>
public static string SetParameter(this UrlHelper helper, string url, string key, string value) {
return helper.SetParameters(url, new Dictionary<string, object> {
{key, value}
});
}
回答by Imran Rizvi
If you really want this you need to redirect to new same page with changed query string as already people answered. This will again load your page,loading page again just for changing querystring that is not good practice.
如果你真的想要这个,你需要重定向到新的相同页面,并更改查询字符串,因为人们已经回答了。这将再次加载您的页面,再次加载页面只是为了更改查询字符串,这是不好的做法。
But Why do you need this?
但是你为什么需要这个?
If you want to change the value of sortBy from 6 to 4 to use everywhere in the application, my suggession is to store the value of query string into some variable or view state and use that view state everywhere.
如果要将 sortBy 的值从 6 更改为 4 以在应用程序中随处使用,我的建议是将查询字符串的值存储到某个变量或视图状态中并随处使用该视图状态。
For e.g.
例如
in Page_Load you can write
在 Page_Load 你可以写
if (!IsPostBack)
{
ViewState["SortBy"] = Request.QueryString["sortBy"];
}
and everywhere else ( even after postback ) you can use ViewState["SortBy"]
和其他任何地方(即使在回发之后)你都可以使用 ViewState["SortBy"]
回答by legrandviking
I think you could perform a replacement of the query string in the following fashion on your server side code.
我认为您可以在服务器端代码上以以下方式替换查询字符串。
NameValueCollection filtered = new NameValueCollection(request.QueryString);
filtered.Remove("SortBy");
filtered.Add("SortBy","4");
回答by m.othman
Retrieve the querystring of sortby, then perform string replaceon the full Url as follow:
检索sortby的查询字符串,然后对完整的 Url执行字符串替换,如下所示:
string sUrl = *retrieve the required complete url*
string sCurrentValue = Request.QueryString["sortby"];
sUrl = sUrl.Replace("&sortby=" + sCurrentValue, "&sortby=" + newvalue);
Let me know how it goes :)
让我知道事情的后续 :)
Good luck
祝你好运
回答by SkonJeet
The query string is passed TO the server. You can deal with the query string as a string all you like - but it won't do anything until the page is ready to read it again (i.e. sent back to the server). So if you're asking how to deal with the value of a query string you just simply access it Request.QueryString["key"]. If you're wanting this 'change' in query string to be considered by the server you just need to effectively reload the page with the new value. So construct the url again page.aspx?id=30for example, and pass this into a redirect method.
查询字符串被传递到服务器。您可以将查询字符串作为您喜欢的字符串处理 - 但在页面准备好再次读取它之前它不会做任何事情(即发送回服务器)。因此,如果您询问如何处理查询字符串的值,您只需访问它 Request.QueryString["key"]。如果您希望服务器考虑查询字符串中的这种“更改”,您只需要使用新值有效地重新加载页面。因此page.aspx?id=30,例如再次构建 url ,并将其传递给重定向方法。
回答by Mathias Nohall
private void UpdateQueryString(string queryString, string value)
{
PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
isreadonly.SetValue(this.Request.QueryString, false, null);
this.Request.QueryString.Set(queryString, value);
isreadonly.SetValue(this.Request.QueryString, true, null);
}

