C# Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Request["key"] vs Request.Params["key"] vs Request.QueryString["key"]
提问by Chris
Request["key"]
vs Request.Params["key"]
vs Request.QueryString["key"]
Request["key"]
对比Request.Params["key"]
对比Request.QueryString["key"]
Which method do you seasoned programmers use? and why?
经验丰富的程序员使用哪种方法?为什么?
采纳答案by btlog
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
.
我推荐Request.QueryString["key"]
。没有很大的差异,以Request["Key"]
供查询字符串但如果你试图从值大(ER)的区别ServerVariables
。Request["Key"]
在QueryString
if null 中查找值,它会查看Form
、 thenCookie
和 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"]
.
使用Params
成本最高。到PARAMS的第一个请求创建一个新的NameValueCollection
,并将每个的QueryString
,Form
,Cookie
和ServerVariables
到这个集合。对于第二个请求,它的性能比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.
话虽如此,几个键的性能差异可以忽略不计。这里的关键是代码应该显示意图,使用Request.QueryString
可以清楚地表明你的意图是什么。
回答by No Refunds No Returns
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.
我总是明确指定集合。如果由于某种原因您想允许覆盖,请为每个人编码“get”并编写一些清晰的代码,显示您选择一个而不是另一个的层次结构。IMO,我不喜欢在没有明确商业理由的情况下从多个来源获取价值。
回答by Phil
I prefer to use Request.QueryString["key"]
because it helps the code-reader know exactlywhere 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.
我更喜欢使用,Request.QueryString["key"]
因为它可以帮助代码阅读器准确地知道您从哪里获取数据。我倾向于不使用Request.Params["key"]
它,因为它可以指代 cookie、查询字符串和其他一些东西;所以用户必须考虑一下。人们需要弄清楚你在想什么的时间越少,维护代码就越容易。
回答by curtisk
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.
HttpRequest.Params
或者Request.Params
从 httprequest 中获取几乎所有内容(查询字符串、表单、cookie 和会话变量),而Request.Querystring
只会拉取查询字符串......这一切都取决于您当时在做什么。
回答by cherishty
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.
请注意,如果您在 web.config 下设置requestValidationMode="4.5",Request.QueryString[“key”]和Request[“key”]都将使用“延迟加载”行为作为设计。
However, somehow Request.Params[“key”]will still trigger validation as the behavior of 4.0 .
然而,不知何故Request.Params[“key”]仍然会触发验证作为 4.0 的行为。
This odd behavior really confuses me for a long time.
这种奇怪的行为真的让我困惑了很长时间。