例如:http://baidu.com/?a=hello&b=world
添加函数输入:("c","kkcat"),输出就是http://baidu.com/?a=hello&b=world&c=kkcat
删除函数输入:("b"),输出就是http://baidu.com/?a=hello
添加函数
public static string QueryParameter(string key, string value)
{
return QueryParameter(new Dictionary<string, string>() { { key, value } },string.Empty);
}
public static string QueryParameter(string key, string value, string path)
{
return QueryParameter(new Dictionary<string, string>() { { key, value } }, path);
}
public static string QueryParameter(Dictionary <string,string> dics,string path)
{
string ret=string.Empty ;
Dictionary<string, string> OldQueryString = new Dictionary<string, string>();
foreach (string key in Page.Request.QueryString.AllKeys)
{
OldQueryString .Add (key,Page.Request .QueryString [key]);
}
foreach (KeyValuePair <string,string> item in dics)
{
if (!OldQueryString.ContainsKey(item.Key))
OldQueryString.Add(item.Key, item.Value);
else
OldQueryString[item.Key] = item.Value;
}
foreach (KeyValuePair<string, string> item in OldQueryString)
{
ret += string.Format("{0}={1}&", item.Key, item.Value);
}
if(ret.EndsWith ("&"))
ret=ret.Substring (0,ret.Length -1);
if (string.IsNullOrEmpty(path))
path = Page.Request.Path;
return Utils.ToUri ( path).ToString () +"?"+ret;
}
public static string QueryParameter(string key, string value)
{
return QueryParameter(new Dictionary<string, string>() { { key, value } },string.Empty);
}
public static string QueryParameter(string key, string value, string path)
{
return QueryParameter(new Dictionary<string, string>() { { key, value } }, path);
}
public static string QueryParameter(Dictionary <string,string> dics,string path)
{
string ret=string.Empty ;
Dictionary<string, string> OldQueryString = new Dictionary<string, string>();
foreach (string key in Page.Request.QueryString.AllKeys)
{
OldQueryString .Add (key,Page.Request .QueryString [key]);
}
foreach (KeyValuePair <string,string> item in dics)
{
if (!OldQueryString.ContainsKey(item.Key))
OldQueryString.Add(item.Key, item.Value);
else
OldQueryString[item.Key] = item.Value;
}
foreach (KeyValuePair<string, string> item in OldQueryString)
{
ret += string.Format("{0}={1}&", item.Key, item.Value);
}
if(ret.EndsWith ("&"))
ret=ret.Substring (0,ret.Length -1);
if (string.IsNullOrEmpty(path))
path = Page.Request.Path;
return Utils.ToUri ( path).ToString () +"?"+ret;
}
删除函数
public static string RemoveQueryParameter(string par)
{
return RemoveQueryParameter(new string[] { par });
}
public static string RemoveQueryParameter(string[] pars)
{
string ret = string.Empty;
Dictionary<string, string> OldQueryString = new Dictionary<string, string>();
foreach (string key in Page.Request.QueryString.AllKeys)
{
bool isAdd = true;
foreach (string s in pars)
{
if (s.ToLower() == key.ToLower())
isAdd = false;
}
if(isAdd )
OldQueryString.Add(key, Page.Request.QueryString[key]);
foreach (KeyValuePair<string, string> item in dics)
{
if (!OldQueryString.ContainsKey(item.Key))
OldQueryString.Add(item.Key, item.Value);
else
OldQueryString[item.Key] = item.Value;
}
foreach (KeyValuePair<string, string> item in OldQueryString)
{
ret += string.Format("{0}={1}&", item.Key, item.Value);
}
if (ret.EndsWith("&"))
ret = ret.Substring(0, ret.Length - 1);
if (string.IsNullOrEmpty(path))
path = Page.Request.Path;
return Utils.ToUri(path).ToString() + "?" + ret;
}
}
public static string RemoveQueryParameter(string par)
{
return RemoveQueryParameter(new string[] { par });
}
public static string RemoveQueryParameter(string[] pars)
{
string ret = string.Empty;
Dictionary<string, string> OldQueryString = new Dictionary<string, string>();
foreach (string key in Page.Request.QueryString.AllKeys)
{
bool isAdd = true;
foreach (string s in pars)
{
if (s.ToLower() == key.ToLower())
isAdd = false;
}
if(isAdd )
OldQueryString.Add(key, Page.Request.QueryString[key]);
foreach (KeyValuePair<string, string> item in dics)
{
if (!OldQueryString.ContainsKey(item.Key))
OldQueryString.Add(item.Key, item.Value);
else
OldQueryString[item.Key] = item.Value;
}
foreach (KeyValuePair<string, string> item in OldQueryString)
{
ret += string.Format("{0}={1}&", item.Key, item.Value);
}
if (ret.EndsWith("&"))
ret = ret.Substring(0, ret.Length - 1);
if (string.IsNullOrEmpty(path))
path = Page.Request.Path;
return Utils.ToUri(path).ToString() + "?" + ret;
}
}