Can any one tell me is there any difference between:
Request.QueryString["id"]
and Request["id"]
If yes which is better to use?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With