C# Request.Params 和 Request.Form 什么时候不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5706/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-07-31 16:24:56  来源:igfitidea点击:

When do Request.Params and Request.Form differ?

提问by Matt Mitchell

I recently encountered a problem where a value was null if accessed with Request.Form but fine if retrieved with Request.Params. What are the differences between these methods that could cause this?

我最近遇到了一个问题,如果使用 Request.Form 访问,则值为 null,但如果使用 Request.Params 检索则很好。这些可能导致这种情况的方法之间有什么区别?

采纳答案by Brandon Wood

Request.Form only includes variables posted through a form, while Request.Params includes both posted form variables and get variables specified as URL parameters.

Request.Form 仅包括通过表单发布的变量,而 Request.Params 包括发布的表单变量和指定为 URL 参数的 get 变量。

回答by Matt Mitchell

The reason was that the value I was retrieving was from a form element, but the submit was done through a link + JQuery, not through a form button submit.

原因是我检索的值来自表单元素,但提交是通过链接 + JQuery 完成的,而不是通过表单按钮提交。

回答by Richard Szalay

Request.Params contains a combination of QueryString, Form, Cookies and ServerVariables (added in that order).

Request.Params 包含 QueryString、Form、Cookie 和 ServerVariables(按该顺序添加)的组合。

The difference is that if you have a form variable called "key1" that is in both the QueryString and Form then Request.Params["key1"] will return the QueryString value and Request.Params.GetValues("key1") will return an array of [querystring-value, form-value].

不同之处在于,如果您在 QueryString 和 Form 中都有一个名为“key1”的表单变量,那么 Request.Params["key1"] 将返回 QueryString 值,Request.Params.GetValues("key1") 将返回一个[查询字符串值,形式值] 的数组。

If there are multiple form values or cookies with the same key then those values will be added to the array returned by GetValues (ie. GetValues will not return a jagged array)

如果有多个表单值或具有相同键的 cookie,则这些值将添加到 GetValues 返回的数组中(即 GetValues 不会返回锯齿状数组)