Parsing URI query strings in Windows 8 metro-style apps

Tuesday, August 21, 2012


Take the URL string http://example.com/?a=foo&b=bar&c=baz. How do we parse its query parameters using C# to get a dictionary or other key-value data structure?

In .NET, you can use HttpUtility.ParseQueryString(), but that function isn't present in WinRT APIs. Fortunately, there is a replacement: WwwFormUrlDecoder. The code below demonstrates its capabilities:

using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Windows.Foundation;

[TestClass]
public class Tests
{
    [TestMethod]
    public void TestWwwFormUrlDecoder()
    {
        Uri uri = new Uri("http://example.com/?a=foo&b=bar&c=baz");
        WwwFormUrlDecoder decoder = new WwwFormUrlDecoder(uri.Query);

        // named parameters
        Assert.AreEqual("foo", decoder.GetFirstValueByName("a"));

        // named parameter that doesn't exist
        Assert.ThrowsException<ArgumentException>(() => {
            decoder.GetFirstValueByName("not_present");
        });

        // number of parameters
        Assert.AreEqual(3, decoder.Count);

        // ordered parameters
        Assert.AreEqual("b", decoder[1].Name);
        Assert.AreEqual("bar", decoder[1].Value);

        // ordered parameter that doesn't exist
        Assert.ThrowsException<ArgumentException>(() => {
            IWwwFormUrlDecoderEntry notPresent = decoder[3];
        });
    }
}

Tags: metro, winrt, csharp | Posted at 15:36 | Comments (2)


Comments

Sandeep on Thursday, August 17, 2017 at 01:22

WebUtility can be used instead of WwwFormUrlDecoder

Lolapoede on Thursday, May 31, 2018 at 08:49

https://t.me/dating2000 asian dating

If I was edible what you would eat at me?

Dimitrovgrad (about one year Melekess) — the city in Russia located on the left coast of the Kuibyshev reservoir at falling of Big Cheremshan's river into it. RUSSIA (Russian Federation) a sudarstvo in Eastern Europe and in Northern Asia. Population more than one million people. The capital — Moscow; large cities. St. Petersburg, Novosibirsk, Nizhny Novgorod, Yekaterinburg, Samara, Omsk, Kazan, Ufa, Chelyabinsk, Rostov-on-Don, Perm, Volgograd. Get acquainted, communicate and meet new people in the city of Dimitrovgrad. Good luck and Love!
At the moment the group is not conducted, but as you see, announcements of acquaintance appear regularly. Therefore, do not forget to get acquainted and respond to new announcements.
stnost, fidelity, decency.
pravoslavny club of acquaintances from!
akomstvo in an evening.
on short number.
Write, we will get acquainted)
So, you formulated a topic of the lesson. Discuss in couple vopros.chy I will study at a lesson? Formulate your purposes of this lesson.
How to be registered on the website "" to strike up acquaintances in Tyumen?
The logopedist gives to children the instruction:
syuzhetnorolevy and director's games, help to master.
Derive pleasure, but do not expect too much. You approach Lesbian acquaintances effortlessly. And also use other opportunities for search of the girl.
Acquaintance purpose. I Look for the romantic relations. Good girl.
They say, the love the street can meet where an ugodno.n, in cafe or on a staircase. But there are places where the probability of such meeting is much higher. If you wish to find long-awaited family happiness as soon as possible, for the aid to you marriage agencies work. Izhevsk – the city in which there is a choice.
Lyudmila Anatolyevna, of course, it is possible! Only I will be glad to help!
Display of wedding images.
c) convex, roundish ()

Add a comment

Name:
Email: (optional, not displayed to public)
URL:

Comment: