Category Archives: C#

Deserialize object from urlencoded data in C#

Ever needed to deserialize application/x-www-form-urlencoded POST-data or querystring-data into C# objects?

Simple usage

    public class Foo
    {
        public string Bar { get; set; }
        public string Baz { get; set; }
        public string Qux { get; set; }
    }
    var serializer = new ParamSerializer();
    var foo = serializer.Deserialize<Foo>("bar=text1&baz=text2&qux=text3");

You can also use it directly on a NameValueCollection, such as Request.Form or Request.QueryString

    var foo2 = serializer.Deserialize<Foo>(Request.Form);
    var foo3 = serializer.Deserialize<Foo>(Request.QueryString);

Sourcecode on GitHub:

https://github.com/unger/SimpleHttpHandler/blob/master/SimpleHttpHandler/RequestHelpers/ParamSerializer.cs

The underlying implementation is a C# port of the Javascript library jQuery.deparam:

https://github.com/chrissrogers/jquery-deparam/blob/master/jquery-deparam.js