Get Querystring variables from a String url in C#


This Code Snippet explains how to get the querystring variables from a string url. You cannot get it done every time by Request.Querystring["Name"], especially when you try to get the querystring variables from a string. This Snippets will help you in that cases.

Think of that you want all the querystring variables from a string, most of us will think of doing by substring or modifying the string.One pretty way to accomplish this by doing so.


There may be some cases where you want to get all the variables from a string. Say that from third party response url. In such cases you can get all the variables using HttpUtility.ParseQueryString


String url = "http://Yourwebsite.in/Default.aspx?name=Tom&Age=20&status=true";
Uri strUri = new Uri(url);
String Query = strUri.Query;



Now we need to parse the querysting and there is a method HttpUtility.ParseQueryString which is used to collect specific data from the string url.



string sName = HttpUtility.ParseQueryString(Query).Get("name");
string sAge = HttpUtility.ParseQueryString(Query).Get("Age");
string sStatus = HttpUtility.ParseQueryString(Query).Get("status");


by the way you can collect all the querystring variables without using substring or other.


Regards,
Velmurugan.P


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: