Request[“key”] vs Request.Params[“key”] vs Request.QueryString[“key”]
Request["key"]
vs Request.Params["key"]
vs Request.QueryString["key"]
你使用哪种方法程序员用?为什么?
Request["key"]
vs Request.Params["key"]
vs Request.QueryString["key"]
Which method do you seasoned programmers use? and why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我推荐
Request.QueryString["key"]
。对于查询字符串,Request["Key"]
没有太大区别,但如果您尝试从ServerVariables
获取值,则有很大的区别代码>.Request["Key"]
在QueryString
中查找值,如果为 null,则查找Form
,然后查找Cookie
最后是ServerVariables
。使用
Params
的成本最高。对 params 的第一个请求创建一个新的NameValueCollection
并添加每个QueryString
、Form
、Cookie
和>ServerVariables
到此集合。对于第二个请求,它的性能比Request["Key"]
更高。话虽如此,几个键的性能差异可以忽略不计。这里的关键是代码应该显示意图,并且使用
Request.QueryString
可以清楚地表明您的意图是什么。I recommend
Request.QueryString["key"]
. There isn't a lot of difference toRequest["Key"]
for a query string but there is a big(er) difference if you are trying to get the value fromServerVariables
.Request["Key"]
looks for a value inQueryString
if null, it looks atForm
, thenCookie
and finallyServerVariables
.Using
Params
is the most costly. The first request to params creates a newNameValueCollection
and adds each of theQueryString
,Form
,Cookie
andServerVariables
to this collection. For the second request on it is more performant thanRequest["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.我更喜欢使用
Request.QueryString["key"]
因为它可以帮助代码阅读器准确了解您从哪里获取数据。我倾向于不使用Request.Params["key"]
因为它可以引用 cookie、查询字符串和其他一些东西;所以用户必须思考一下。人们需要弄清楚你在想什么的时间越少,维护代码就越容易。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 useRequest.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.HttpRequest.Params
或Request.Params
从 httprequest 获取几乎所有内容(查询字符串、表单、cookie 和会话变量),而Request.Querystring
只会拉取查询字符串...一切都取决于您当时在做什么。HttpRequest.Params
orRequest.Params
gets just about everything (querystring, form, cookie and session variables) from the httprequest, whereasRequest.Querystring
only will pull the querystring... all depends on what you are doing at the time.我总是明确指定集合。如果出于某种原因您想要允许覆盖,请为每一项编写“get”代码,并编写一些清晰的代码来显示您选择其中一项的层次结构。在我看来,如果没有明确的商业理由,我不喜欢从多个来源获取价值。
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.
请注意,如果您在 web.config 下设置 requestValidationMode="4.5",则 Request.QueryString[“key”] 和 Request[“key” ] 将使用“延迟加载”行为作为设计。
然而,不知何故 Request.Params[“key”] 仍然会像 4.0 的行为一样触发验证。
这种奇怪的行为确实让我困惑了很长一段时间。
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.
总是发现使用 request.querystring 来定位 url 参数更有用,它省去了尝试跟踪可以从其他各个位置获取的其他值的麻烦。
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.