简体   繁体   English

ASP.NET Core http post [FromBody] 在 3.1 中损坏,在 2.2 中使用

[英]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.

相关问题 使用ASP.NET Core 2.2的HTTP / 2 - HTTP/2 with ASP.NET Core 2.2 发布到 ASP.NET Core 3.1 Web 应用程序时,“[FromBody]MyClass 数据”通常为空 - When posting to an ASP.NET Core 3.1 web app, "[FromBody]MyClass data" is often null ASP.net 核心 3.1,添加对 controller 动作自动绑定 [FromBody] 参数的依赖注入支持 - ASP.net core 3.1, add dependency injection support for controller actions auto bound [FromBody] parameters 与 ASP.NET Core 中的 FromBody 混淆 - Confused with FromBody in ASP.NET Core ASP.NET 核心 - [FromBody] 的奇怪行为 - ASP.NET Core - weird behavior with [FromBody] ASP.Net Core 2.2 通过 NGINX 部署在 Linux 上,POST 请求返回 HTTP 错误 400 - ASP.Net Core 2.2 deployed on Linux via NGINX, POST requests returning HTTP Error 400 通过[FromBody]到MongoDB GeoJsonObjectModel成员的POST / PUT到ASP.Net Core始终为null - POST/PUT to ASP.Net Core with [FromBody] to a MongoDB GeoJsonObjectModel member is always null 在ASP.NET Core MVC中无法调用具有类参数带有[FromBody]属性的类的API Post方法 - Unable to call API Post Method that has a class parameter with [FromBody] attribute in asp.net core mvc 从 .NET Core 2.2 迁移到 3.1 后,不会在 ASP.NET Core 自定义 AuthenticationHandler 中忽略 AllowAnonymous - AllowAnonymous is not ignored in ASP.NET Core custom AuthenticationHandler after migrating from .NET Core 2.2 to 3.1 将 .Net Core 2.2 和 ASP.NET 样板 4.5 升级到 .Net Core 3.1 和 ASP.NET 样板 5.13 时出现 ObjectMapper 错误 - ObjectMapper Error when Upgrade .Net Core 2.2 & ASP.NET Boilerplate 4.5 to .Net Core 3.1 & ASP.NET Boilerplate 5.13
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM