Request[“key”] vs Request.Params[“key”] vs Request.QueryString[“key”]

发布于 2024-08-22 07:20:37 字数 140 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

路还长,别太狂 2024-08-29 07:20:37

我推荐 Request.QueryString["key"]。对于查询字符串,Request["Key"] 没有太大区别,但如果您尝试从 ServerVariables 获取值,则有很大的区别代码>. Request["Key"]QueryString 中查找值,如果为 null,则查找 Form,然后查找 Cookie最后是ServerVariables

使用 Params 的成本最高。对 params 的第一个请求创建一个新的 NameValueCollection 并添加每个 QueryStringFormCookie>ServerVariables 到此集合。对于第二个请求,它的性能比 Request["Key"] 更高。

话虽如此,几个键的性能差异可以忽略不计。这里的关键是代码应该显示意图,并且使用 Request.QueryString 可以清楚地表明您的意图是什么。

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.

幼儿园老大 2024-08-29 07:20:37

我更喜欢使用 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 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.

少钕鈤記 2024-08-29 07:20:37

HttpRequest.ParamsRequest.Params 从 httprequest 获取几乎所有内容(查询字符串、表单、cookie 和会话变量),而 Request.Querystring只会拉取查询字符串...一切都取决于您当时在做什么。

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.

梦太阳 2024-08-29 07:20:37

我总是明确指定集合。如果出于某种原因您想要允许覆盖,请为每一项编写“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.

隐诗 2024-08-29 07:20:37

请注意,如果您在 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.

莫言歌 2024-08-29 07:20:37

总是发现使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文