HttpUtility.ParseQueryString 的反向函数

发布于 2024-08-27 20:50:06 字数 255 浏览 8 评论 0原文

.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

莫相离 2024-09-03 20:50:06

System.Collections.Specialized.NameValueCollection 确实支持此功能,但派生内部类System.Web.HttpValueCollection 支持 >(通过重写ToString())。

不幸的是(在内部)您无法直接实例化此类,但由 HttpUtility.ParseQueryString() 返回一个类(您可以使用 String.Empty 调用它,但不能使用<代码>空)。

拥有 HttpValueCollection 后,您可以通过调用 Add() 从原始 NameValueCollection 填充它,然后最后调用 ToString()

var nameValueCollection = new NameValueCollection {{"a","b"},{"c","d"}};
var httpValueCollection = System.Web.HttpUtility.ParseQueryString(String.Empty);
httpValueCollection.Add(nameValueCollection);
var qs = httpValueCollection.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 class System.Web.HttpValueCollection DOES (by overriding ToString()).

Unfortunately (being internal) you cannot instantiate this class directly, but one is returned by HttpUtility.ParseQueryString() (and you can call this with String.Empty, but not Null).

Once you have a HttpValueCollection, you can fill it from your original NameValueCollection by calling Add(), before finally calling ToString().

var nameValueCollection = new NameValueCollection {{"a","b"},{"c","d"}};
var httpValueCollection = System.Web.HttpUtility.ParseQueryString(String.Empty);
httpValueCollection.Add(nameValueCollection);
var qs = httpValueCollection.ToString();

nameValueCollection.ToString() = "System.Collections.Specialized.NameValueCollection"
httpValueCollection.ToString() = "a=b&c=d"

£烟消云散 2024-09-03 20:50:06

NameValueCollection 有一个自动 ToString() 方法,该方法会自动将所有元素作为查询字符串写出。

你不需要自己写。

var querystringCollection = HttpUtility.ParseQueryString("test=value1&test=value2");

var output = querystringCollection.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.

var querystringCollection = HttpUtility.ParseQueryString("test=value1&test=value2");

var output = querystringCollection.ToString(); 

output = "test=value1&test=value2"

豆芽 2024-09-03 20:50:06

我发现 UriBuilderHttpUtility 类的组合满足我操作查询参数的要求。 Uri 类本身是不够的,特别是它的 Query 属性是只读的。

var uriBuilder = new UriBuilder("http://example.com/something?param1=whatever");
var queryParameters = HttpUtility.ParseQueryString(uriBuilder.Query);
queryParameters.Add("param2", "whatever2");
queryParameters.Add("param3", "whatever2");
uriBuilder.Query = queryParameters.ToString();
var urlString = uriBuilder.Uri.ToString();

上面的代码生成 URL 字符串: http://example.com/ some?param1=whatever¶m2=whatever2¶m3=whatever2

请注意,ToString() 通过 Uri 属性进行操作,否则输出字符串中将包含显式端口 80。

很高兴能够使用框架类来完成所有这些工作,而不必编写我们自己的代码。

I found that a combination of UriBuilder and HttpUtility classes meets my requirements to manipulate query parameters. The Uri class on its own is not enough, particularly as its Query property is read only.

var uriBuilder = new UriBuilder("http://example.com/something?param1=whatever");
var queryParameters = HttpUtility.ParseQueryString(uriBuilder.Query);
queryParameters.Add("param2", "whatever2");
queryParameters.Add("param3", "whatever2");
uriBuilder.Query = queryParameters.ToString();
var urlString = uriBuilder.Uri.ToString();

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.

木森分化 2024-09-03 20:50:06

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/

庆幸我还是我 2024-09-03 20:50:06

这是我一直使用的两个非常有用的功能:

    private string GetQueryStringParameterValue(Uri url, string key)
    {
        return HttpUtility.ParseQueryString(url.Query.TrimStart('?'))[key];
    }

    private Uri SetQueryStringParameterValue(Uri url, string key, string value)
    {
        var parameters = HttpUtility.ParseQueryString(url.Query.TrimStart('?'));

        parameters[key] = value;

        var uriBuilder = new UriBuilder(url) { Query = parameters.ToString() };

        return uriBuilder.Uri;
    }

Here are 2 very useful functions that I use all the time:

    private string GetQueryStringParameterValue(Uri url, string key)
    {
        return HttpUtility.ParseQueryString(url.Query.TrimStart('?'))[key];
    }

    private Uri SetQueryStringParameterValue(Uri url, string key, string value)
    {
        var parameters = HttpUtility.ParseQueryString(url.Query.TrimStart('?'));

        parameters[key] = value;

        var uriBuilder = new UriBuilder(url) { Query = parameters.ToString() };

        return uriBuilder.Uri;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文