Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Request.QueryString["id"] and Request["id"]

Can any one tell me is there any difference between:

Request.QueryString["id"] and Request["id"]

If yes which is better to use?

like image 593
ANP Avatar asked Aug 10 '10 11:08

ANP


People also ask

What is request QueryString?

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.

What is request QueryString in asp net?

The ASP Request. QueryString Collection is used to get the value of the variable that will be stored in the HTTP query string from the URL.


3 Answers

Request["id"] gets a value from the QueryString, Form, Cookies, or ServerVariables collections. The order in which they are searched is not specified in the documentation but when you take a look at the source code you'll see that it is the order in which they are mentioned.

So if you know where your variable resides, which you usually do, it's better to use the more specific option.

like image 93
Ronald Wildenberg Avatar answered Sep 28 '22 01:09

Ronald Wildenberg


the Request collection is a superset of QueryString, along with some more data related to the current request.

so as for "better" - I'd advise you'd be precise and explicit (i.e. use QueryString) to avoid the surprise factor, when you get unexpected results just to realise that you used a key for which a given request did not supply a query-string value, but it exists on some other collection.

like image 35
Ken Egozi Avatar answered Sep 28 '22 01:09

Ken Egozi


According to the documentation the HttpRequest indexer

The QueryString, Form, Cookies, or ServerVariables collection member specified in the key parameter.

I'd prefer using Request.QueryString["id"] since it's more explicit where the value is coming from.

like image 41
Lee Avatar answered Sep 28 '22 01:09

Lee