HttpUtility.ParseQueryString 的反向函数
.Net 的 System.Web.HttpUtility 类定义了以下函数: 将查询字符串解析为 NameValueCollection
:
public static NameValueCollection ParseQueryString(string query);
是否有任何函数可以执行相反的操作(即将 NameValueCollection
转换为查询字符串)?
.Net's System.Web.HttpUtility
class defines the following function to parse a query string into a NameValueCollection
:
public static NameValueCollection ParseQueryString(string query);
Is there any function to do the reverse (i.e. to convert a NameValueCollection
into a query string)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
System.Collections.Specialized.NameValueCollection
确实不支持此功能,但派生内部类System.Web.HttpValueCollection
支持 >(通过重写ToString()
)。不幸的是(在内部)您无法直接实例化此类,但由
HttpUtility.ParseQueryString()
返回一个类(您可以使用String.Empty
调用它,但不能使用<代码>空)。拥有
HttpValueCollection
后,您可以通过调用Add()
从原始NameValueCollection
填充它,然后最后调用ToString()
。nameValueCollection.ToString() = "System.Collections.Specialized.NameValueCollection"
httpValueCollection.ToString() = "a=b&c=d"
System.Collections.Specialized.NameValueCollection
does NOT support this, but a derived internal classSystem.Web.HttpValueCollection
DOES (by overridingToString()
).Unfortunately (being internal) you cannot instantiate this class directly, but one is returned by
HttpUtility.ParseQueryString()
(and you can call this withString.Empty
, but notNull
).Once you have a
HttpValueCollection
, you can fill it from your originalNameValueCollection
by callingAdd()
, before finally callingToString()
.nameValueCollection.ToString() = "System.Collections.Specialized.NameValueCollection"
httpValueCollection.ToString() = "a=b&c=d"
NameValueCollection 有一个自动 ToString() 方法,该方法会自动将所有元素作为查询字符串写出。
你不需要自己写。
输出=“测试=值1&测试=值2”
A NameValueCollection has an automatic ToString() method that will write all your elements out as a querystring automatically.
you don't need to write your own.
output = "test=value1&test=value2"
我发现
UriBuilder
和HttpUtility
类的组合满足我操作查询参数的要求。Uri
类本身是不够的,特别是它的Query
属性是只读的。上面的代码生成 URL 字符串: http://example.com/ some?param1=whatever¶m2=whatever2¶m3=whatever2
请注意,ToString() 通过
Uri
属性进行操作,否则输出字符串中将包含显式端口 80。很高兴能够使用框架类来完成所有这些工作,而不必编写我们自己的代码。
I found that a combination of
UriBuilder
andHttpUtility
classes meets my requirements to manipulate query parameters. TheUri
class on its own is not enough, particularly as itsQuery
property is read only.The above code results in the URL string: http://example.com/something?param1=whatever¶m2=whatever2¶m3=whatever2
Note that the ToString() goes via a
Uri
property, otherwise the output string would have an explicit port 80 in it.It's nice to be able to do all this using framework classes and not have to write our own code.
我不认为有内置的,但这里有一个如何实现 http://blog.leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/
I don't think there is a built in one, but here is an example of how to implement http://blog.leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/
这是我一直使用的两个非常有用的功能:
Here are 2 very useful functions that I use all the time: