Parse query string

17/06/2015 15:02

using System.Web;

  

string queryString = https://bla.com?a=1&b=3&source=jfkjdskfjsdkfjdksf;


 NameValueCollection qscoll = HttpUtility.ParseQueryString(queryString);
 string first = qscoll.Keys[0]; //https://bla.com?a
 string firstValue = qscoll[first]; //1

 string second = qscoll.Keys[1]; //b
 string secondValue = qscoll[second]; //3


//For redirection

string currentUrl = "https://bla.com?Source=https://google.com";
string queryString = currentUrl.Substring(currentUrl.IndexOf('?'), currentUrl.Length - currentUrl.IndexOf('?'));
NameValueCollection qscoll = HttpUtility.ParseQueryString(queryString);
string firstValue = qscoll["Source"];


public string GetQueryParameterFromUrl(string url, string parameterName)
 {
  string parameterValue = string.Empty;

  string queryString = url.Substring(url.IndexOf('?'), url.Length - url.IndexOf('?'));

  NameValueCollection qscoll = HttpUtility.ParseQueryString(queryString);
  parameterValue = qscoll[parameterName];

  return parameterValue;
 }