#region = SafeAddQueryToURL =
///
/// Add a query to an URL.
/// if the URL has not any query,then append the query key and value to it.
/// if the URL has some queries, then check it if exists the query key already,replace the value, or append the key and value
/// if the URL has any fragment, append fragments to the URL end.
///
///
/// string s = "http://blog.csdn.net/leewhoee/ a=1&b=2&c=3#tag";
/// WL(SafeRemoveQueryFromURL("a",s));
/// WL(SafeRemoveQueryFromURL("b",s));
/// WL(SafeRemoveQueryFromURL("c",s));
/// WL(SafeAddQueryToURL("d","new",s));
/// WL(SafeAddQueryToURL("a","newvalue",s));
/// 输出如下:
/// http://blog.csdn.net/leewhoee/ b=2&c=3#tag
/// http://blog.csdn.net/leewhoee/ a=1&c=3#tag
/// http://blog.csdn.net/leewhoee/ a=1&b=2#tag
/// http://blog.csdn.net/leewhoee/ a=1&b=2&c=3&d=new#tag
/// http://blog.csdn.net/leewhoee/ a=newvalue&b=2&c=3#tag
///
public static string SafeAddQueryToURL(string key, string value, string url)
{
int fragPos = url.LastIndexOf("#");
string fragment = string.Empty;
if (fragPos > -1)
{
fragment = url.Substring(fragPos);
url = url.Substring(0, fragPos);
}
int querystart = url.IndexOf(" ");
if (querystart < 0)
{
url += " " + key + "=" + value;
}
else
{
Regex reg = new Regex(@"( <=[&\ ])" + key + @"=[^\s]*", RegexOptions.Compiled);
if (reg.IsMatch(url))
url = reg.Replace(url, key + "=" + value);
else
url += "&" + key + "=" + value;
}
return url + fragment;
}
#endregion
#region = SafeRemoveQueryFromURL =
///
/// Remove a query from url
///
///
///
///
public static string SafeRemoveQueryFromURL(string key, string url)
{
Regex reg