Thursday, 8 July 2010

REPLACE QUERY PARAMETER VALUE

  1. Response.Redirect(ReplaceQueryParameterValue(Request.RawUrl, "all", "1"));
  2. private string ReplaceQueryStringValue(string url, string key, string value)
  3. {
  4. return url.Contains(key) ? Regex.Replace(url, @"([?&]" + key + ")=[^?&]+", "$1=" + value) : (url + (url.Contains("?") ? "&" : "?") + key + "=" + value);
  5. }
  6. private string ReplaceQueryParameterValue(string query, string name, string value)
  7. {
  8. string output;
  9. if (query.Contains(name + "="))
  10. {
  11. output = System.Text.RegularExpressions.Regex.Replace(query,
  12. @"(&|\?)" + name + @"=[0-9A-Za-z\+\!\*\(\)\'\-\$\,\.]+", @"$1" + name + "=" + value,
  13. System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  14. }
  15. else
  16. output = query + (query.Contains("?") ? "&" : "?") + name + "=" + value;
  17. return output;
  18. }

No comments:

Post a Comment