网页API表单数据收集 [英] Web API Form Data Collection
本文介绍了网页API表单数据收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想张贴的形式连载值控制器(网页API自主机)。
我不明白为什么NameValueCollection中不正确绑定。
使用jQuery客户端:
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:
// 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) {
}
});
}
服务器端使用Web API自我主持人:
Server-side using Web API Self Host:
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);
}
}
}
非常感谢。
推荐答案
,尝试FormDataCollection。
Instead of NameValueCollection, try FormDataCollection.
<一个href=\"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
这篇关于网页API表单数据收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文