/ Published in: C#
Expand |
Embed | Plain Text
Response.Redirect(ReplaceQueryParameterValue(Request.RawUrl, "all", "1")); private string ReplaceQueryStringValue(string url, string key, string value) { return url.Contains(key) ? Regex.Replace(url, @"([?&]" + key + ")=[^?&]+", "$1=" + value) : (url + (url.Contains("?") ? "&" : "?") + key + "=" + value); } private string ReplaceQueryParameterValue(string query, string name, string value) { string output; if (query.Contains(name + "=")) { output = System.Text.RegularExpressions.Regex.Replace(query, @"(&|\?)" + name + @"=[0-9A-Za-z\+\!\*\(\)\'\-\$\,\.]+", @"$1" + name + "=" + value, System.Text.RegularExpressions.RegexOptions.IgnoreCase); } else output = query + (query.Contains("?") ? "&" : "?") + name + "=" + value; return output; }
Comments
Subscribe to comments
You need to login to post a comment.

Hello, thanks for doing this! Howe can this be changed to support replacing a value that is encoded URL?? Any help would be appreciated.
Even if i replace it with an empty string (""), the result is that "HTTP" gets replaced, and the rest of the parameter value stays. coverimage=http%3A%2F%2Fgalleryserver....
coverimage=%3A%2F%2Fgalleryserver....
For example: If I start with this: "http://www.zazzle.com/api/create/at-238965785060790379?rf=238345325100453699&ax=DesignBlast&cg=196484196445896905&ed=true&br=true&rcg=196484196445896905&ds=true&continueUrl=http%3A%2F%2Fwww.zazzle.com%2Fmainepictures&rut=Go%20back%20to%20MainePictures's%20store&fwd=ProductPage&coverimage=http%3A%2F%2Fgalleryserver.greatmainepictures.com%2Fgs%2Fhandler%2Fgetmediaobject.ashx%3Fmoid%3D6%26dt%3D3%26g%3D0"
And want to replace the parameter "coverimage" with even just an empty String, ("") I get this as a result:
"http://www.zazzle.com/api/create/at-238965785060790379?rf=238345325100453699&ax=DesignBlast&cg=196484196445896905&ed=true&br=true&rcg=196484196445896905&ds=true&continueUrl=http%3A%2F%2Fwww.zazzle.com%2Fmainepictures&rut=Go%20back%20to%20MainePictures's%20store&fwd=ProductPage&coverimage=%3A%2F%2Fgalleryserver.greatmainepictures.com%2Fgs%2Fhandler%2Fgetmediaobject.ashx%3Fmoid%3D6%26dt%3D3%26g%3D0"
Did you try URLDecoding it?