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

I just want to get the full request from url.

Request.QueryString 

Request.ServerVariables["QUERY_STRING"]

Can I use any of them? which way is preferred?

thanks

+6
source share
3 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 that allows you to retrieve individual items. Using the following syntax:

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

This collection is created from the ServerVariables collection. The values ​​in this collection are automatically UrlDecoded.

So, if you call Request.QueryString.ToString() , this is essentially the same as Request.ServerVariables["QUERY_STRING"] , but with UrlDecoding.
Therefore, 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

+6
source

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

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

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

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

+1
source

If you call Request.QueryString ["Whatever"] than UrlDecode is executed automatically. See Does Request.Querystring execute to automatically decrypt the string? . So be careful with your spaces,% 20, ampersands, etc.

Regards, Michael

0
source

Source: https://habr.com/ru/post/945688/


All Articles