Get URL Parameters using LINQ


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. Dictionary<String, String> result = new Dictionary<string, string>();
  2. String urlString = "http://www.jwize.com?param1=valu1&param2=value2";
  3.  
  4. var query = from match in urlString.Split('?').Where(m => m.Contains('='))
  5. .SelectMany(pr => pr.Split('&'))
  6. where match.Contains('=')
  7. select new KeyValuePair<string, String>(
  8. match.Split('=')[0],
  9. match.Split('=')[1]);
  10. query.ToList().ForEach(kvp => result.Add(kvp.Key, kvp.Value));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.