Now how do I get these parameter values and map them with appropriate variables. I mean in case of a single parameter, we'd do a Request.QueryString["search"].ToString().
I mean there are multiple parameters and we'd want to map them to the correct variables so that we can implement some business logic with the parameters. Sorry if my query appears to be of basic nature, but I am slightly confused. I suppose we need to implement
some looping mechanism based on the position/index of the '&' character. I could be wrong though.
Need some active assistance on this issue from our fabulous forum members.
Many Thanks in anticipation.
There are more wonders in this world of ours than you can wonder.....
The second approach uses forech from the IEnumerable interface to iterate. Which one according to you should I with perspective to faster execution time?
Please suggest.
Thanks.
There are more wonders in this world of ours than you can wonder.....
From my opinion both are same. In the first example, i have declared a namevalue collection and assigned the query string and in the second i just iterate through the keys of querystring. You can go with the second approach as it is clean compare to the
first.
Regards,
Senthil Kumar Sundaram
Marked as answer by PGChoudhury on Dec 20, 2012 09:35 AM
PGChoudhury
Member
11 Points
131 Posts
Retrieving QueryTring Parameters --
Dec 20, 2012 06:25 AM|LINK
Greetings.
I have a situation which I will describe here. Suppose I have a url which looks like the following:
http://www.psltr.com?search=1&openId=41&sessionId=33949&SVID=102&_Xuid=29.
Now how do I get these parameter values and map them with appropriate variables. I mean in case of a single parameter, we'd do a Request.QueryString["search"].ToString().
I mean there are multiple parameters and we'd want to map them to the correct variables so that we can implement some business logic with the parameters. Sorry if my query appears to be of basic nature, but I am slightly confused. I suppose we need to implement some looping mechanism based on the position/index of the '&' character. I could be wrong though.
Need some active assistance on this issue from our fabulous forum members.
Many Thanks in anticipation.
senthilwaits
Contributor
3832 Points
651 Posts
Re: Retrieving QueryTring Parameters --
Dec 20, 2012 06:33 AM|LINK
The Request.QueryString property is a name value collection, so you can use a for/foreach loop to iterate through that.
Refer http://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring.aspx
You can use some thing like
int loop1, loop2; // Load NameValueCollection object. NameValueCollection coll=Request.QueryString; // Get names of all keys into a string array. String[] arr1 = coll.AllKeys; for (loop1 = 0; loop1 < arr1.Length; loop1++) { Response.Write("Key: " + Server.HtmlEncode(arr1[loop1]) + "<br>"); String[] arr2 = coll.GetValues(arr1[loop1]); for (loop2 = 0; loop2 < arr2.Length; loop2++) { Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>"); } }foreach (string key in Request.QueryString.Keys) { Response.Write(key+ ” “+Request.QueryString[key]+”<br/>”); }Please refer
Senthil Kumar Sundaram
PGChoudhury
Member
11 Points
131 Posts
Re: Retrieving QueryTring Parameters --
Dec 20, 2012 07:05 AM|LINK
Hi.
Many thanks for the code snippet.
The second approach uses forech from the IEnumerable interface to iterate. Which one according to you should I with perspective to faster execution time?
Please suggest.
Thanks.
senthilwaits
Contributor
3832 Points
651 Posts
Re: Retrieving QueryTring Parameters --
Dec 20, 2012 07:53 AM|LINK
From my opinion both are same. In the first example, i have declared a namevalue collection and assigned the query string and in the second i just iterate through the keys of querystring. You can go with the second approach as it is clean compare to the first.
Senthil Kumar Sundaram
PGChoudhury
Member
11 Points
131 Posts
Re: Retrieving QueryTring Parameters --
Dec 20, 2012 09:36 AM|LINK
Thanks for the clarification.
I will go for the 2nd approach. It's cleaner.
Cheers.