酢ろぐ!

カレーが嫌いなスマートフォンアプリプログラマのブログ。

WindowsストアアプリでLINQを使ってクエリ文字列(URLパラメータ)をパースする

下記のようによく見かけるURL形式があります。

http://example.com/action?param1=hoge&param2=hige

「?」以降のparam1=hoge&param2=higeの部分をクエリ文字列(URLパラメータ)と呼びます。クエリ文字列をLINQを使ってパースしてみましょう。

Dictionary<string, string> SplitUrlParam(string response)
{
    var splitted = response.Split('&')
        .Select(s => s.Split('='))
        .ToDictionary(s => s.First(), s => s.Last());
    return splitted;
}