asp.net URL handles Two gadgets method _ Practical Tips

Source: Internet
Author: User
Sometimes we have to operate a URL address query parameters, in order not to break the original structure of the URL, we generally can not directly in the back of the URL plus &query=value, especially in our URL has more than one parameter, this kind of handling more trouble.
The following two small methods are specifically used to add a query parameter to a URL or delete a query parameter, the two methods hide the original URL has no parameters, is not originally have this parameter, there is no fragment (#anchor) these details and processing
/**////<summary>
Add a query to a 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 VA Lue
If the URL has any fragment, append fragments to the URL end.
</summary>
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;
}
/**////<summary>
Remove a query from URL
</summary>
<param name= "Key" ></param>
<param name= "url" ></param>
<returns></returns>
public static string Saferemovequeryfromurl (String key,string URL)
{
Regex reg = new Regex (@ "[&\?]" +key+@ "=[^\s&#]*&?", regexoptions.compiled);
Return Reg. Replace (url,new matchevaluator (Putawaygarbagefromurl));
}
private static string Putawaygarbagefromurl (match match)
{
String value = Match. Value;
if (value. EndsWith ("&"))
return value. Substring (0,1);
Else
return string. Empty;
}

Test:
string s = "Http://www.cnblogs.com/?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));
The output is as follows:
Http://www.cnblogs.com/?b=2&c=3#tag
Http://www.cnblogs.com/?a=1&c=3#tag
Http://www.cnblogs.com/?a=1&b=2#tag
Http://www.cnblogs.com/?a=1&b=2&c=3&d=new#tag
Http://www.cnblogs.com/?a=newvalue&b=2&c=3#tag
Related Article

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.