Nick Allen – Tungle139

They are indeed different, but they are of the same kind, as there is no duality in Truth

Creating a QueryString utility Class

Building up query strings is quite a common task in web development but there is currently no utility class in .Net that neatly facilitates this. So I thought I’d have a go at creating one that nicely encapsulates the build process and makes use of Generic Collections. And here is what I came up with!

public class QueryString
{
    private Dictionary _Params = new Dictionary();

    public overide ToString()
    {
        List returnParams = new List();

        foreach (KeyValuePair param in _Params)
        {
            returnParams.Add(String.Format("{0}={1}", param.Key, param.Value));
        }

        return "?" + String.Join("&", returnParams.ToArray());
    }

    public void Add(string key, string value)
    {
        _Params.Add(key, HttpUtility.UrlEncode(value));
    }
}

This can be used like so

QueryString query = new QueryString();

query.Add("param1", "value1");
query.Add("param2", "value2");

return query.ToString();

Filed under: Code

Pages

Del.icio.us Bookmarks

 

May 2012
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28293031  

Where Am I?

Flickr Photostream

Times Square

Statue of Liberty

Madison Square Garden

The Legendary Fat Beats NYC

The Legendary Fat Beats NYC

More Photos
Follow

Get every new post delivered to your Inbox.