1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NameValueCollerction からクエリストリング文字列を生成する

Posted at

クエリストリングとは

URL の後ろによくあるやつ
http://xxxxxxx.xx.jp?var1=val1&var2=val2

? から後ろのやつ

C#でどうやってつくるの?

System.Collections.Specialized.NameValueCollection を使って作れるよ!

やってみた(ダメパターン)

Sample.cs
var col = new System.Collections.Specialized.NameValueCollection();
col.Add("var1", "val1");
col.Add("var2", "val2");
Console.WriteLine(col.ToString());

よし、できたぞ!

・・・

image.png

あれ??

やってみた(成功パターン)

どうやら System.Web.HttpUtility.ParseQueryString メソッドで返却される NameValueCollection を使わないとうまくいかないらしい。
おそらく、返却される NameValueCollection は純粋な NameValueCollection ではないため(だと思う)。

Sample.cs
string queryString = "";
var col = System.Web.HttpUtility.ParseQueryString(queryString);
col.Add("var1", "val1");
col.Add("var2", "val2");
Console.WriteLine(col.ToString());

image.png

OK!

まとめ

気をつけようね。

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?