tags:

views:

60

answers:

1

I have recently started using Request("key") instead of Request.QueryString("key") to access my querystring values. However I have read that:

'Gets the specified object from the System.Web.HttpRequest.Cookies, System.Web.HttpRequest.Form, System.Web.HttpRequest.QueryString, System.Web.HttpRequest.ServerVariables'

Therefore, if I have a querystring key and cookie key which are the same, which value is returned?

+9  A: 

They're checked in the following order:

  1. QueryString
  2. Form
  3. Cookies
  4. ServerVariables

The search is short-circuited, so as soon as a matching key is found the value is returned.

So, to answer your question, a matching QueryString item takes precedence over Cookies.

LukeH
For more information, see Scott Hanselman's excellent blog post on this a while ago at http://www.hanselman.com/blog/ASPNETParamsCollectionVsQueryStringFormsVsRequestindexAndDoubleDecoding.aspx
Tomas Lycken
Thanks Luke. The only problem I see with using this is that if I was to do an If Statement on whether or not a querystring existed, using 'request' would return true if a cookie of the same name existed. Therefore I think I'll stick with 'Request.Querystring'. Plus, it'll be useful for other developers to know straight away exactly where this information is being requested from. However, it's good to know that this short-hand way is available for future use, cheers.
Curt