These two functions are processed relative to the URL of the current request.
Example: http://baidu.com /? A = hello & B = world
Add function input :( "c", "kkcat"), the output is http://baidu.com /? A = hello & B = world & c = kkcat
Delete function input :( "B"), the output is the http://baidu.com /? A = hello add function
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;
} Delete A Function
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;
}
}