Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is difference between Request.QueryString and Request.ServerVariables["QUERY_STRING"]

Tags:

c#

.net

asp.net

i just want to get full querystring from url.

Request.QueryString 

Request.ServerVariables["QUERY_STRING"]

Can i use any one of these? which way is preferred?

Thanks

like image 302
Maddy Avatar asked May 23 '13 13:05

Maddy


2 Answers

Request.ServerVariables["QUERY_STRING"] contains the entire query string, that is everything after the question mark but before the fragment identifier #

http://msdn.microsoft.com/en-us/library/ms525396(v=vs.90).aspx

Request.QueryString Contains a collection allowing you to get individual elements. Using following syntax:

Request.QueryString(variable)[(index)|.Count]

This collection is generated from the ServerVariables collection. The values in this collection are automaticly UrlDecoded.

So if you call Request.QueryString.ToString(), it is inherently the same as Request.ServerVariables["QUERY_STRING"], but with UrlDecoding.
So you should use this as it is safer.

Request.QueryString(variable)[(index)|.Count]

http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx

like image 52
Willem D'Haeseleer Avatar answered Oct 21 '22 08:10

Willem D'Haeseleer


http://msdn.microsoft.com/en-us/library/ms524784(v=vs.90).aspx

The QueryString collection is a parsed version of the QUERY_STRING variable in the ServerVariables collection. It enables you to retrieve the QUERY_STRING variable by name. The value of Request.QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request.QueryString(parameter).Count. If a variable does not have multiple data sets associated with it, the count is 1. If the variable is not found, the count is 0.

To reference a QueryString variable in one of multiple data sets, you specify a value for index. The index parameter can be any value between 1 and Request.QueryString(variable).Count. If you reference one of multiple QueryString variables without specifying a value for index, the data is returned as a comma-delimited string.

When you use parameters with Request.QueryString, the server parses the parameters sent to the request and returns the specified data. If your application requires unparsed QueryString data, you can retrieve it by calling Request.QueryString without any parameters.

like image 29
Maddy Avatar answered Oct 21 '22 08:10

Maddy