更改单个URL查询字符串值 [英] Change Single URL query string value

查看:135
本文介绍了更改单个URL查询字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET页面这需要在查询字符串的一些参数:

I have an ASP.NET page which takes a number of parameters in the query string:

search.aspx?q=123&source=WebSearch

这将显示搜索结果的第一页。现在该网页的呈现内,我想显示的一组,允许用户跳转至搜索结果中的不同的网页的链接。我可以简单地通过追加做到这一点&安培;页= 1 &放大器;页= 2

This would display the first page of search results. Now within the rendering of that page, I want to display a set of links that allow the user to jump to different pages within the search results. I can do this simply by append &page=1 or &page=2 etc.

它变得复杂的是,我想preserve从原来的页面输入的查询字符串除了一个我试图改变每个参数。有可能是由其他组件,我试图取代可能或不可能已经定义的值所使用的网址等参数:

Where it gets complicated is that I want to preserve the input query string from the original page for every parameter except the one that I'm trying to change. There may be other parameters in the url used by other components and the value I'm trying to replace may or may not already be defined:

search.aspx?q=123&source=WebSearch&page=1&Theme=Blue

在这种情况下生成一个链接到结果的下一页,我想改变页= 1 页= 2 ,同时使查询字符串的其余部分保持不变。

In this case to generate a link to the next page of results, I want to change page=1 to page=2 while leaving the rest of the query string unchanged.

有一个内置的方式做到这一点,或者我需要做的所有字符串解析/手动重组?

Is there a builtin way to do this, or do I need to do all of the string parsing/recombining manually?

推荐答案

您不能修改<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$pquest.querystring.aspx\">QueryString直接,因为它是只读的。您将需要得到的值,修改它们,然后把他们重新走到一起。试试这个:

You can't modify the QueryString directly as it is readonly. You will need to get the values, modify them, then put them back together. Try this:

var nameValues = HttpUtility.ParseQueryString(Request.QueryString.ToString());
nameValues.Set("page", "2");
string url = Request.Url.AbsolutePath;
string updatedQueryString = "?" + nameValues.ToString();
Response.Redirect(url + updatedQueryString);

ParseQueryString 方法回报一<一href=\"http://msdn.microsoft.com/en-us/library/system.collections.specialized.namevaluecollection.aspx\"><$c$c>NameValueCollection (其实它真的返回 HttpValueCollection 其中EN codeS的结果,<一个href=\"http://stackoverflow.com/questions/3865975/namevaluecollection-to-url-query/3866105#3866105\">as我在回答另一个问题提)。然后,您可以使用设置方法更新值。您也可以使用添加方法来添加一个新的,或删除来删除该值。最后,调用的ToString()上的名称的NameValueCollection 返回名称值对在名1 =值1&放大器; 2 =值2 查询字符串准备格式。一旦你拥有它添加到URL和重定向。

The ParseQueryString method returns a NameValueCollection (actually it really returns a HttpValueCollection which encodes the results, as I mention in an answer to another question). You can then use the Set method to update a value. You can also use the Add method to add a new one, or Remove to remove a value. Finally, calling ToString() on the name NameValueCollection returns the name value pairs in a name1=value1&name2=value2 querystring ready format. Once you have that append it to the URL and redirect.

另外,您可以添加新的密钥,或修改现有的,使用索引:

Alternately, you can add a new key, or modify an existing one, using the indexer:

nameValues["temp"] = "hello!"; // add "temp" if it didn't exist
nameValues["temp"] = "hello, world!"; // overwrite "temp"
nameValues.Remove("temp"); // can't remove via indexer

您可能需要添加一个使用System.Collections.Specialized; 来利用的NameValueCollection 类的。

You may need to add a using System.Collections.Specialized; to make use of the NameValueCollection class.

这篇关于更改单个URL查询字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆