C# – Convert querystring from/to object


I have this (simplified) class:

public class StarBuildParams
    public int BaseNo { get; set; }
    public int Width { get; set; }

And I have to transform instances of it to a querystring like this:


Additionally I have to transform such a querystring back in an object of that class.

I know that this is pretty much what a modelbinder does, but I don't have the controller context in my situation (some deep buried class running in a thread).

So, is there a simple way to convert a object in a query string and back without having a controller context?

It would be great to use the modelbinding but I don't know how.

Best Solution

A solution with Newtonsoft Json serializer and linq:

string responseString = "BaseNo=5&Width=100";
var dict = HttpUtility.ParseQueryString(responseString);
string json = JsonConvert.SerializeObject(dict.Cast<string>().ToDictionary(k => k, v => dict[v]));
StarBuildParams respObj = JsonConvert.DeserializeObject<StarBuildParams>(json);