Tags

, ,

For websites that require some kind of state management or filtering on client-side data, the QueryString is indispensable. For example, when you want to pass a filtering on location data to a Google Maps control on your page, the QueryString is a convenient means to achieve this. For this purpose it is a good idea to make a static QueryHelperClass, that contains a few methods and all available keys. In this way you are able to manage your QueryString in one central place and you will not accidentally have duplicate keys like this: “www.mywebsite.com?searchTown=Amsterdam&searchCity=Amsterdam”

The QueryString is nothing more than a NameValueCollection that is appended to the URL. So a method that adds to the QueryString could use a NameValueCollection to add new values.

public static class QueryStringHelper
{
	public static string AddParameterCollectionToCurrentUrl(NameValueCollection parameters)
	{
		NameValueCollection querystringCollection  = MergeQueryStrings(parameters);
		StringBuilder queryString = new StringBuilder();
		string url = HttpContext.Current.Request.Url.AbsolutePath;
		bool isFirstParameter = true;

		foreach (string name in querystringCollection.Keys)
		{
			if(isFirstParameter)
			{
				queryString.Append("?");

				isFirstParameter = false;
			}
			else
			{
				queryString.Append("&");
			}
			queryString.Append(name);
			queryString.Append("=");
			queryString.Append(querystringCollection[name]);
		}

		return url + queryString;
	}

	private static NameValueCollection MergeQueryStrings(NameValueCollection querystringCollection)
	{
		NameValueCollection originalQueryString = new NameValueCollection(HttpContext.Current.Request.QueryString);

		foreach (string name in querystringCollection.Keys)
		{
			if (!string.IsNullOrWhiteSpace(querystringCollection[name]))
			{
				if(string.IsNullOrWhiteSpace(originalQueryString[name]))
				{
					originalQueryString.Add(name, querystringCollection[name]);
				}
				else
				{
					originalQueryString.Remove(name);

					originalQueryString.Add(name, querystringCollection[name]);
				}
			}
			else
			{
				originalQueryString.Remove(name);
			}
		}

		return originalQueryString;
	}
}

This method AddParameterCollectionToCurrentUr(), with the help of MergeQueryStrings(), contains some logic to make sure that the first parameter in the QueryString is preceded by a “?” and the rest by a “&”. Furthermore it makes sure that parameters could be overwritten if they should be updated, and that they are deleted if there is no value. This is because the methods will often be called while the field should contain the value is empty. A Google Maps control for example does not always have to have a location filter, it should have a default view.

Because you often only have to add one parameter to the QueryString, we should also add a convenience method AddParameterToCurrentUrl():

public static string AddParameterToCurrentUrl(string key, string value)
{
	NameValueCollection queryStringCollection = new NameValueCollection();
	queryStringCollection.Add(key, value);

	string urlWithQueryString = AddParameterCollectionToCurrentUrl(queryStringCollection);

	return urlWithQueryString;
}

In the introduction of this article I mentioned adding all available keys to this static class. This makes sure that all possible keys could be managed in one single location, and that keys could be passed in the method AddParameterToCurrentUrl() as strongly typed. So add a few keys to the class as public static properties:

public static string KeyLocationFilter
{
	get { return "locationfilter"; }
}

public static string KeyLocationSearch
{
	get { return "locationsearch"; }
}

And then the QueryStringHelper could be used in your web application as follows:

private void ButtonSearch_Click(object sender, EventArgs e)
{
	string searchLocation = TextBoxSearch.Text;
	url = QueryStringHelper.AddParameterToCurrentUrl(QueryStringHelper.KeyLocationSearch, searchLocation);

	Response.Redirect(url);
}
Advertisements