Asp.net Api未反序列化Post中子数组的属性
我有一个在浏览器中从json正确反序列化的复杂对象,但是当我用jQuery发回相同的数据时,数组的子对象没有反序列化——它们都设置为默认值 我尝试过将集合类型从List更改为IEnumerables,但没有任何改变。有趣的是,我得到的元素数量是正确的 这也是一个CORS的帖子,但我没有其他问题 模型Asp.net Api未反序列化Post中子数组的属性,asp.net,asp.net-web-api,http-post,asp.net-core,Asp.net,Asp.net Web Api,Http Post,Asp.net Core,我有一个在浏览器中从json正确反序列化的复杂对象,但是当我用jQuery发回相同的数据时,数组的子对象没有反序列化——它们都设置为默认值 我尝试过将集合类型从List更改为IEnumerables,但没有任何改变。有趣的是,我得到的元素数量是正确的 这也是一个CORS的帖子,但我没有其他问题 模型 public class Document { public int DocumentId { get; set; } public DocumentType DocumentTy
public class Document
{
public int DocumentId { get; set; }
public DocumentType DocumentType { get; set; }
public string Name { get; set; }
public string Path { get; set; }
public IEnumerable<DocumentField> Fields { get; set; }
}
public class DocumentField
{
public int FieldId { get; set; }
public string Path { get; set; }
public IEnumerable<FieldRelationship> RelatedFields { get; set; }
}
public class FieldRelationship
{
public int RelationshipId { get; set; }
public int FieldId { get; set; }
}
jQuery
var res = $.get('http://localhost:11786/api/documents/1');
res.then(function (data) {
$.ajax({
type: 'POST',
url: 'http://localhost:11786/api/documents/',
crossDomain: true,
data: data,
success: function (responseData, textStatus, jqXHR) {
console.log('yes');
},
error: function (responseData, textStatus, errorThrown) {
console.log('failed');
}
});
});
响应主体的外观:
Fields[0][FieldId]=1410
&Fields[0][Path]=TestField
&Fields[0][RelatedFields][0][RelationshipId]=1
&Fields[0][RelatedFields][0][FieldId]=503
&Fields[0][RelatedFields][1][FieldId]=501
&Fields[1][Path]=cookies
&DocumentId=1
&DocumentType=2
&Name=Test Document
&Path=test.docx
结果C#Documents实例中的值是什么
DocumentId: 1
DocumentType: 2
Name: "Test Document"
Path: "test.docx"
Fields: [{
FieldId: 0, <--- Bad
Path: null, <--- Bad
RelatedFields: null, <--- Bad
}, {
FieldId: 0, <--- Bad
Path: null, <--- Bad
RelatedFields: null
}]
如果我使用angular的$http.post(
$http.post('http://localhost:11786/api/documents/,res.data);
)并将[FromBody]
属性添加到方法参数中,我会得到正确的结果。尽管我尝试了上述四种方法,但似乎我在$.ajax中传递给数据的内容并不正确。看起来您遇到了一个已知的问题,即模型绑定不支持绑定到JQuery formurlencoded数组格式。以下问题正在跟踪它
奇怪的是,你不是故意设置
[FromBody]
。我的问题是,当你使用ajax调用时,为什么不以json格式发送数据……是为了避免CORS飞行前请求吗?将contentType
添加到你的ajax设置对象json中。字符串化你的文档对象,将json字符串发送到服务器:
var doc={DocumentId:1};
var jsonDoc=JSON.stringify(doc);
ajax('api/test'{
键入:“POST”,
资料来源:jsonDoc,
contentType:'应用程序/json'
})
.完成(功能(数据、状态、jqr){
警报(状态);
})
它应该可以工作。我尝试发送JSON,但结果总是为空。我知道mvc asp.net5合并了mvc和API,您只需添加[FromBody]即可处理这两个问题。@Stef无论我如何尝试使用[FromBody],我都会得到空值。我尝试以每种方式传递数据,仅作为对象,作为JSON.stringized字符串,作为对象作为参数{“”:data}和作为stringized参数{“”:str}。娜达。@KiranChalla+1,因为关于问题的根源你是对的。你知道如何修复它吗?添加contentType:“application/json”对我的[FromBody]属性起到了作用。@daniel gimenez,
Document
是一个复杂类型,web api将从请求体提取复杂参数,[FromBody]
属性不是必需的,我们只需要将json发布到控制器。
DocumentId: 1
DocumentType: 2
Name: "Test Document"
Path: "test.docx"
Fields: [{
FieldId: 0, <--- Bad
Path: null, <--- Bad
RelatedFields: null, <--- Bad
}, {
FieldId: 0, <--- Bad
Path: null, <--- Bad
RelatedFields: null
}]
ajax.data [FromBody] Result
--------- ---------- ------
data false Props set, array count correct, array object props null
JSON.stringfy(data) false Object not null, but props null
{ "": data } false Object not null, but props null
"=" + JSON.stringfy false Object not null, but props null
data true null
JSON.stringfy(data) true null
{ "": data } true null
"=" + JSON.stringfy true null