7

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

3 Answers 3

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

if i do not use any parameter with Request.QueryString, it returns full querystring
I don't think you can call Request.QueryString without parameters, but you can call ToString()
1

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.

Comments

0

If you call Request.QueryString["Whatever"] than UrlDecode is automatically executed. See does Request.Querystring automatically url decode a string? . So be careful with your spaces, %20, ampersands etc.

Regards, Michael

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.