0
0

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 1 year has passed since last update.

URLのクエリパラメータを少し楽に記述する

Posted at

NuGetでMicrosoft.AspNetCore.WebUtilitiesをインストールする。
QueryHelpers.AddQueryStringを使用することで、クエリパラメータ生成を行える。

var uri = QueryHelpers.AddQueryString("http://example.com/access", new Dictionary<string, string>
{
    { "parameter1", "'hogehoge'" },
    { "parameter2", "'hugahuga'" },
});
// http://example.com/access?parameter1=%27hogehoge%27&parameter2=%27hugahuga%27

従来は以下の方法でクエリパラメータ生成を行っていた。
文字列操作を行いたいだけなのに非同期処理を用いている点が気になる。

var endodindedParameters =
    await new FormUrlEncodedContent(new Dictionary<string, string>
    {
        { "parameter1", "'hogehoge'" },
        { "parameter2", "'hugahuga'" },
    })
    .ReadAsStringAsync();
var uri = $"http://example.com/access?{endodindedParameters}";
// http://example.com/access?parameter1=%27hogehoge%27&parameter2=%27hugahuga%27
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?