Language: C#
SetUrlEncodedKeys for Query String injection
/// <summary> /// Allows setting of a value in a UrlEncoded string. If the key doesn't exist /// a new one is set, if it exists it's replaced with the new value. /// </summary> /// <param name="urlEncoded">A UrlEncoded string of key value pairs</param> /// <param name="key"></param> /// <param name="value"></param> /// <returns></returns> public static string SetUrlEncodedKey(string urlEncoded, string key, string value) { if (!urlEncoded.EndsWith("?") && !urlEncoded.EndsWith("&")) urlEncoded += "&"; Match match = Regex.Match(urlEncoded, "[?|&]" + key + "=.*?&"); if (match == null || string.IsNullOrEmpty(match.Value)) urlEncoded = urlEncoded + key + "=" + UrlEncode(value) + "&"; else urlEncoded = urlEncoded.Replace(match.Value, match.Value.Substring(0, 1) + key + "=" + UrlEncode(value) + "&"); return urlEncoded.TrimEnd('&'); }
Tags:
Description:
Using this code for a paging component that needs to inject Page No's and other parameters into new URL query strings.
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

