[英]ASP.NET Core http post [FromBody] broken in 3.1, used to work in 2.2
I am having all kinds of errors migrating from ASP.NET Core 2.2, to 3.1.我在从 ASP.NET Core 2.2 迁移到 3.1 时遇到了各种错误。
My latest error is that the object I am receiving via a Http Post is null.我的最新错误是我通过 Http Post 接收的对象为空。
Here is the code that receives the object, and in this case the model is null.这是接收对象的代码,在这种情况下模型为空。
[HttpPost]
public async Task<IActionResult> MyAction([FromBody] BoardMoveModel model)
The model class:模型类:
public class BoardMoveModel
{
public int BoardId { get; set; }
public int TicketId { get; set; }
public int DestinationStatusId { get; set; }
public int SourceStatusId { get; set; }
public IEnumerable<int> SourceStatusList {get; set;}
public IEnumerable<int> DestinationStatusList {get; set;}
}
And the Javascript code:和 Javascript 代码:
var data = {
TicketId: el.id,
DestinationStatusId: targetStatusId,
SourceStatusId: sourceStatusId,
SourceStatusList: sourceList,
DestinationStatusList: destList
};
$.ajax({
async: true,
url: "/Area/Board/MyAction",
type: "POST",
contentType: "application/json; charset=utf-8",
headers: {
RequestVerificationToken:
$('input:hidden[name="__RequestVerificationToken"]').val()
},
data: JSON.stringify(data)
}).done(function(result) {
Here is the actual JSON Payload:这是实际的 JSON 有效负载:
{
"TicketId":"150",
"DestinationStatusId":"5",
"SourceStatusId":"6",
"SourceStatusList":[],
"DestinationStatusList":["140","150"]
}
Model is null because it's invalid for binding.模型为空,因为它对绑定无效。 In JSON you are passing in string values while model requires integers.
在 JSON 中,您传入字符串值,而模型需要整数。 Change either to match and it should work.
更改为匹配,它应该可以工作。 Binding is by default case sensitive, but it shouldn't be a problem with code you've provided.
默认情况下,绑定区分大小写,但您提供的代码应该不会有问题。
Try sending this as application/json body尝试将其作为应用程序/json正文发送
{
"TicketId":150,
"DestinationStatusId":5,
"SourceStatusId":6,
"SourceStatusList":[],
"DestinationStatusList":[140,150]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.