Previously I've posted a few Helper Classes . This post describes my QueryStringHelper class.
using System;
using System.Web;
using System.Collections.Specialized;
using System.Text;
using System.Collections;
/// <summary>
/// Summary description for QueryStringHelper
/// </summary>
public class QueryStringHelper
{
//Consider to use HttpUtility.ParseQueryString
// openSource alternative in MONO http://dsrg.mff.cuni.cz/projects/mono/diffs/showdiff.php?old=2004-11-05&new=2004-11-30&fileIndex=5036&bench=
//17/1/2005 AdditionalParameters in DNN NavigateUrl
public static string[] QueryStringAsStringArray(string sQueryString)
{ // remove leading "?", if exist
string s = sQueryString;
if (s.StartsWith("?")) { s = s.TrimStart('?'); }
string[] asRet = s.Split('&');
return asRet;
}
public static string RemoveParameterFromUrl(string url, string sKey)
{
Uri uri = new Uri(url);
string sQuery = uri.Query;
sQuery = sQuery.TrimStart('?');//http://lists.ximian.com/pipermail/mono-devel-list/2003-September/002082.html
sQuery = RemoveParameterFromQueryString(sQuery, sKey);
//
UriBuilder ub = new UriBuilder(uri);
//if (sQuery.Length>0) sQuery="?"+ sQuery;
ub.Query = sQuery;//Set inserts "?" automatically
return ub.ToString();
}
public static string RemoveParameterFromQueryString(string QueryString, string sKey)
{
NameValueCollection coll = HttpUtility.ParseQueryString(QueryString);
coll.Remove(sKey);
string sRet = QueryStringCollectionToString(coll);
return sRet;
}
public static string GetParameterFromUrl(string url, string sKey)
{
string sQuery = QueryStringFromUrl(url);
NameValueCollection coll = HttpUtility.ParseQueryString(sQuery);
string sRet = coll[sKey];
return sRet; //can return null
}
public static string QueryStringFromUrl(string url)
{ //simpler than using Uri as in RemoveParameterFromUrl
string sQuery = "";
if (url.IndexOf('?') >= 0)
{
sQuery = StringHelper.RightAfter(url, "?");
}
return sQuery;
}
public static string GetParameterFromQueryString(string QueryString, string sKey)
{
NameValueCollection coll = HttpUtility.ParseQueryString(QueryString);
string sRet = coll[sKey];
return sRet; //can return null
}
//if key is empty, nothing will be added
public static string AddQueryStringParameter(string QueryString, string sKey, string sValue)
{
string ParametersWithValues = ParameterWithValue(sKey, sValue);
return AddQueryStringParameter(QueryString, ParametersWithValues);
}
//if key is empty, nothing will be added
public static string AddQueryStringParameter(string QueryString, String ParametersWithValues)
{
StringBuilder builder1 = new StringBuilder(QueryString);
// if (urlencoded)
// {
// text1 = HttpUtility.UrlEncodeUnicode(text1);
// }
// string sToAdd = ((sKey != null) && (sKey.Length > 0) ? sKey + "=" : "");
if (ParametersWithValues.Length > 0)
{
if (builder1.Length > 0) builder1.Append('&');
builder1.Append(ParametersWithValues);
}
return builder1.ToString();
}
public static string AddUrlQueryStringParameters(string Url, string ParametersWithValues)
{//created 5/1/2005
//UriBuilder ub = new UriBuilder(new Uri(Url)); //'Unfortunatel, doesn't work for relative URLS
//17/1/2006 support relative URIs
int nQuestionPos = Url.IndexOf('?');
string sQuery = ""; string sUrlWithoutQuery = Url;
if (nQuestionPos >= 0)
{
sQuery = Url.Remove(0, nQuestionPos);
sUrlWithoutQuery = Url.Remove(nQuestionPos);
//TODO move fragment to the end if applicable
}
// string sQuery = ub.Query;
if (ParametersWithValues.Length > 0)
{
if (sQuery.Length > 0 && !StringHelper.EndsWith(sQuery, '&') && !StringHelper.StartsWith(ParametersWithValues, '&')) sQuery += '&';
sQuery += (ParametersWithValues);
}
sQuery = sQuery.TrimStart('?');//http://lists.ximian.com/pipermail/mono-devel-list/2003-September/002082.html
//ub.Query = sQuery;
//return ub.ToString();//note that port:80 will be added
string sRet = sUrlWithoutQuery;
if (sQuery.Length > 0)
{
sRet += '?' + sQuery;
}
return sRet;
}
public static string AddUrlParameter(string Url, string sKey, string sValue)
{
string ParametersWithValues = ParameterWithValue(sKey, sValue);
return AddUrlQueryStringParameters(Url, ParametersWithValues);
}
//if key is empty, nothing will be added
public static string ParameterWithValue(string sKey, string sValue)
{
// if (urlencoded)
// {
// text1 = HttpUtility.UrlEncodeUnicode(text1);
// }
string sRet = ((sKey != null) && (sKey.Length > 0) ? sKey + "=" : "");
if (sRet.Length > 0)
{
sRet += sValue;
}
return sRet;
}
//TODO move to NameValueCollectionHelper
public static NameValueCollection AddParameterIfNotEmpty(NameValueCollection coll, string sKey, string sValue)
{
if (!String.IsNullOrEmpty(sValue))
{
coll.Add(sKey, sValue);
}
return coll;
}
}