我应该使用 Request.Params 而不是显式执行 Request.Form 吗?
我的所有代码都使用 Request.Form 。如果我需要查询字符串,我也会明确地点击它。在代码审查中,我可能应该使用 Params 集合来代替。
我认为这是最佳实践,直接点击适当的集合。我正在寻找对论点的某一方或另一方的一些强化。
I have been using Request.Form for all my code. And if I need querystring I hit that explicitly too. It came up in a code review that I should probably use the Params collection instead.
I thought it was a best practice, to hit the appropriate collection directly. I am looking for some reinforcement to one side or the other of the argument.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用Request.Form更安全。这将防止用户仅通过更改 URL 来“试验”已发布的表单参数。使用 Request.Form 并不能保证“真正的黑客”的安全,但恕我直言,最好使用 Form 集合。
It is more secure to use Request.Form. This will prevent users from "experimenting" with posted form parameters simply by changing the URL. Using Request.Form doesn't make this secure for "real hackers", but IMHO it's better to use the Form collection.
通过使用请求下的属性,您可以将检索范围缩小到正确的集合(这对于可读性和性能来说是一件好事)。我认为你的方法是最佳实践,并且我自己也遵循它。
By using the properties under the request you are narrowing down the your retrieval to the proper collection (which is a good thing for readability and performance). I consider your approach to be a best practice and follow it myself.
我一直用
请求.Form("参数")
或者
Request.QueryString("Param")
这纯粹是为了更易于阅读的语法。我严重怀疑这会对性能产生影响。
I have always used
Request.Form("Param")
or
Request.QueryString("Param")
This is purely down to a syntax which is easier to read. I seriously doubt there is a performance impact.
我唯一使用
Request.Params
而不是Form
或Querystring
的情况是,如果我不知道参数的方法是否为传递进来。从上下文来看,十年来我只在愤怒中使用过一次
Request.Params
:)Kindness,
D
The only time I use
Request.Params
instead ofForm
orQuerystring
is if I don't know whether the method by which the parameters will be passed in.To put that in context, in 10 years I have used
Request.Params
in anger only once :)Kindness,
D
我认为最好明确使用 Form 和 QueryString 集合,除非您明确尝试在应用程序中定义灵活的行为,例如在搜索表单中,您可能希望在 URL 中定义搜索参数或将搜索参数保存在 cookie 中(例如分页)偏好。
I think it's better to use the Form and QueryString collections explicitly unless you're explicitly trying to define flexible behavior in your application like in a search form where you might want to have the search parameters definable in a URL or saved in cookies such as pagination preferences.
我会明确使用 Request.Form 和 Request.QueryString 。原因是两者不可互换。查询字符串用于 HTTP Get 请求,FORM 变量用于 HTTP POST 请求。
获取请求通常适用于请求数据的情况,例如进行谷歌搜索,搜索词位于查询字符串中。发布是指您将数据发送到网络服务器进行处理或存储。因此,当我说两者不可互换时,我的意思是您无法在不破坏功能的情况下将页面从使用 GET 更改为 POST。
所以恕我直言,页面的实现可以非常清楚地反映出您希望通过 GET 或 POST 请求调用它的事实。
/皮特
I would use Request.Form and Request.QueryString explicitly. The reason is that the two are not interchangable. The query string is used for HTTP Get requests, and FORM variables for HTTP post requests.
Get requests are typically applicable where you are requesting data, e.g. do a google search, the search words are in the query string. The post are when you are sending data to the web server for processing or storing. So when I say that the two are not interchangable I mean that you cannot change the page from using a GET to a POST without breaking functionality.
So IMHO, the implementation of the page can quite clearly reflect the fact that you intend it to be called by a GET or a POST request.
/Pete