Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]
Asked Answered
D

6

75

Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]

Which method do you seasoned programmers use? and why?

Distal answered 22/2, 2010 at 17:45 Comment(0)
S
123

I recommend Request.QueryString["key"]. There isn't a lot of difference to Request["Key"] for a query string but there is a big(er) difference if you are trying to get the value from ServerVariables. Request["Key"] looks for a value in QueryString if null, it looks at Form, then Cookie and finally ServerVariables.

Using Params is the most costly. The first request to params creates a new NameValueCollection and adds each of the QueryString, Form, Cookie and ServerVariables to this collection. For the second request on it is more performant than Request["Key"].

Having said that the performance difference for a couple of keys is fairly negligable. The key here is code should show intent and using Request.QueryString makes it clear what your intent is.

Strade answered 22/2, 2010 at 18:6 Comment(2)
Thanks, you just answered my question as to why cookie values are popping out of my Request.Params[index].Ergot
Great that you noted that a new NameValueCollection is created on first call, now my error and stack trace makes sense :)Gerlachovka
P
24

I prefer to use Request.QueryString["key"] because it helps the code-reader know exactly where you are getting the data from. I tend not to use Request.Params["key"] because it could refer to a cookie, query string and a few other things; so the user has to think a little. The less time someone needs to figure out what you are thinking, the easier it is to maintain the code.

Passageway answered 22/2, 2010 at 17:53 Comment(3)
I think the issue is really where the the data are coming from. It's fine to use Request.Params["key"] as long as you actually don't care whether it's coming from the query string or from somewhere else.Bellyache
If you know which collection the value is contained in, look there. No use making the Request object search all the collections(for performance reasons if nothing else).Maturity
@Will Vousden. Nice point. Your point is very true when you first write the code and the idea is fresh in your head. Fast-Forward two years from now- you have a nasty bug, the code is very hairy/complex and you just want to focus on fixing the problem! Not having to do hunt for the meaning of a variable populated with "Request.Params["key"]" (query string, session variable or something else)can make things a tad easier. It is not huge or make-or-break and you can always look it up; but little things like that go a long way.Passageway
K
7

HttpRequest.Params or Request.Params gets just about everything (querystring, form, cookie and session variables) from the httprequest, whereas Request.Querystring only will pull the querystring... all depends on what you are doing at the time.

Kaleena answered 22/2, 2010 at 17:54 Comment(0)
E
5

I always explicitly specify the collection. If for some reason you want to allow overrides, code the "get" for each one and write some clear code that shows your hierarchy for choosing one over the other. IMO, I dislike getting a value from multiple sources without a clear business reason for so doing.

Emrick answered 22/2, 2010 at 17:52 Comment(0)
E
1

As a kindly notice, If you set requestValidationMode="4.5" under web.config, both Request.QueryString[“key”] and Request[“key”] will use "lazy loading" behavior as design.

However, somehow Request.Params[“key”] will still trigger validation as the behavior of 4.0 .

This odd behavior really confuses me for a long time.

Elmoelmore answered 6/6, 2017 at 5:58 Comment(0)
R
0

Always found it more useful to target url params with the request.querystring, it saves the headache of trying to track down other values it can grab from various other locations.

Recording answered 25/8, 2022 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.