Created
September 25, 2011 00:11
-
-
Save SimplyChris/1240025 to your computer and use it in GitHub Desktop.
Query String Helper Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class QueryStringHelper | |
{ | |
private IDictionary _dictionary; | |
public int Count | |
{ | |
get { return _dictionary.Count; } | |
} | |
public String HtmlStringValue | |
{ | |
get { return GetHtmlStringValue(); } | |
} | |
public QueryStringHelper () | |
{ | |
_dictionary = new Dictionary<string,string>(); | |
} | |
public void Add(string key, string value) | |
{ | |
if (_dictionary.Contains(key)) | |
{ | |
throw new Exception(String.Format(("QueryStringHelper.Add: Key {0} already exists in the dictionary"), key)); | |
} | |
_dictionary.Add(key, value); | |
} | |
private String GetHtmlStringValue () | |
{ | |
if (_dictionary.Count == 0) | |
return String.Empty; | |
var htmlString = String.Empty; | |
foreach (KeyValuePair<String, String> entry in (Dictionary<String, String>)_dictionary) | |
{ | |
if (htmlString.Length == 0) | |
htmlString += "?"; | |
else | |
htmlString += "&"; | |
htmlString += String.Format("{0}={1}", entry.Key, entry.Value); | |
} | |
return htmlString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment