Add or delete parameters in the query string

Source: Internet
Author: User
Tags allkeys
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.