views:

143

answers:

6

Can any one tell me is there any difference between:

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

If yes which is better to use?

+7  A: 

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.

Ronald Wildenberg
-1 from me, for now, as the documentation *does not* specify the order in which the collections are accessed. If the docs match the implementation, that's pure luck, as I noted in my answer, it could change. Remove the "in this order" and I'll be more than happy to remove the -1 =)
Rob
-1 removed, there's another bit of documentation at http://msdn.microsoft.com/en-us/library/ms524948%28VS.90%29.aspx (thanks to @John for his answer pointing it out) which indicates that the collections are searched in a specific order. It'd be nice if MS mentioned that in the documentation that's specifically for Request.Item, eh! :-/
Rob
I removed the 'in this order' part because this is undocumented. Looking at the source code, however, you'll see that it uses the order specified. But you're right, this is an implementation detail :)
Ronald Wildenberg
And now a +1. Poor StackOverflow vote hamsters will be exhausted by the end of this! ;=)
Rob
Scanning through this, I was considering a -1 because it now says it's undocumented, when it in fact is.
Jon Hanna
@Jon It's not in the documentation for HttpRequest.Item (http://msdn.microsoft.com/en-us/library/system.web.httprequest.item.aspx). Can you tell me where I can find it, then I'll update my answer.
Ronald Wildenberg
No, I mis-spoke, as discussed by Rob and myself elsewhere on this page.
Jon Hanna
+1  A: 

Request.QueryString["id"] will return the value of an item in the query string that has a key of id, whereas Request["id"] will return an item from one of Request.QueryString, Request.Form, Request.Cookies, or Request.ServerVariables.

It's worth mentioning that the documentation for Request.Item (which is what you're actually accessing when you call Request["id"]) does not specify the order in which the collections will be searched, so you could theoretically receive a different result depending on which version of asp.net you're running on.

If you know that the value you want is in your query string, it's always better to use Request.QueryString["id"] to access it, rather than Request["id"].

Rob
+1  A: 

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.

Lee
+1  A: 

Request.QueryString["id"] looks into the collection passed per QueryString. Request.Item["id"] looks into all Collections(QueryString, Form, Cookies, or ServerVariables). Therefore the QueryString Property should be preferred when possible because it is smaller.

Tim Schmelter
+2  A: 

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.

Ken Egozi
+1 for answering the whole question. I've certainly seen code get confused when something ended up being read from cookies instead of querystring.
Jon Hanna
+1  A: 

According to Reflector.Net, Request["id"] is defined as:

public string this[string key]
{
    get
    {
        string str = this.QueryString[key];
        if (str != null)
        {
            return str;
        }
        str = this.Form[key];
        if (str != null)
        {
            return str;
        }
        HttpCookie cookie = this.Cookies[key];
        if (cookie != null)
        {
            return cookie.Value;
        }
        str = this.ServerVariables[key];
        if (str != null)
        {
            return str;
        }
        return null;
    }
}
Enigmativity
You're relying on an implementation detail here...
Rob
@Rob As opposed to relying on documentation. Which would you trust? :)
bzlm
@bzlm, the search order is *undocumented*, therefore relying on it is unwise at best, potentially catastrophic at worst. If something is not documented, you have to assume that it's *subject to change at any point*.
Rob
The search order is documented.
Jon Hanna
@Jon Hanna, where?
Rob
@Rob My bad, it's implied in that being the order given in the documentation, but not explicitly stated that that's the order used. Now, it is the only sensible order when one thinks on it, and it builds on the documented feature in "classic" ASP, but you are right that it is not explicit. A flaw in the docs IMO.
Jon Hanna
@Jon, that's what I first thought when I saw the erroneous link to the classic asp docs (that MS had put it on the summary page and *not* the `HttpRequest.Item` page). I agree that it's the only sensible order and *is* implied, but trusting implementation details has burnt me enough times to learn from that mistake =)
Rob
@Rob. Yes, since it's implied I'd say it's "badly documented" rather than "undocumented", but it's still bad documentation. That said, even if spec'd as this, the fact that it may hit a collection you don't expect it to is problem enough in practice.
Jon Hanna