C# Web API 表单数据收集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13605358/
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
Web API Form Data Collection
提问by user1824269
I'm trying to post form serialized values to controller (Web API Self Host). I cannot understand why the NameValueCollection is not correctly bound. Client-side using jQuery:
我正在尝试将表单序列化值发布到控制器(Web API 自主机)。我不明白为什么 NameValueCollection 没有正确绑定。客户端使用 jQuery:
// Form Submit Handler
$( '#form-parameters' ).submit(function (event) {
event.preventDefault();
var formData = $(this).serialize();
// Post serialized form data
postAssemblyParameters(formData);
});
// Post Form Data to controller test
function postAssemblyParameters(formData){
$.ajax({
url: http://localhost/api/test/1,
type: 'POST',
data: formData,
dataType: 'application/x-www-form-urlencoded',
success: function(x3d) {
},
error: function(xhr) {
}
});
}
Server-side using Web API Self Host:
服务器端使用 Web API 自托管:
public void Post([FromUri] int id, [FromBody] NameValueCollection formData)
{
Console.WriteLine(id); // OK
// Collection is NULL
foreach (var key in formData.AllKeys)
{
foreach (var val in formData.GetValues(key))
{
Console.WriteLine(key + ": " + val);
}
}
}
Many thanks.
非常感谢。
采纳答案by scott stone
Instead of NameValueCollection, try FormDataCollection.
而不是 NameValueCollection,试试 FormDataCollection。
http://weblogs.asp.net/cibrax/archive/2012/08/10/binding-form-data-in-asp-net-web-api.aspx
http://weblogs.asp.net/cibrax/archive/2012/08/10/binding-form-data-in-asp-net-web-api.aspx
回答by Brad
Thanks Scott. Your answer was just what I needed. Based on it, I wrote the following little utility method that will come in handy as it pulls all the data values out of the stream (using the FormDataCollection) and puts them into a NamedValueCollection (which you can access in any order you want).
谢谢斯科特。你的回答正是我所需要的。基于它,我编写了以下小实用程序方法,它会派上用场,因为它将所有数据值从流中提取出来(使用 FormDataCollection)并将它们放入 NamedValueCollection(您可以按您想要的任何顺序访问)。
/// <summary>
/// Copy the values contained in the given FormDataCollection into
/// a NameValueCollection instance.
/// </summary>
/// <param name="formDataCollection">The FormDataCollection instance. (required, but can be empty)</param>
/// <returns>The NameValueCollection. Never returned null, but may be empty.</returns>
public static NameValueCollection Convert(FormDataCollection formDataCollection)
{
Validate.IsNotNull("formDataCollection", formDataCollection);
IEnumerator<KeyValuePair<string, string>> pairs = formDataCollection.GetEnumerator();
NameValueCollection collection = new NameValueCollection();
while (pairs.MoveNext())
{
KeyValuePair<string, string> pair = pairs.Current;
collection.Add(pair.Key, pair.Value);
}
return collection
}
回答by HoefMeistert
Or you can use the build "ReadAsNameValueCollection", like this :
或者您可以使用构建“ReadAsNameValueCollection”,如下所示:
NameValueCollection MyNameValueCollection = formData.ReadAsNameValueCollection();

