【问题标题】:Cannot post Json array to web core api无法将 Json 数组发布到 Web 核心 API
【发布时间】:2018-01-25 11:52:35
【问题描述】:

使用此代码,我试图将 Json 数组发布到我的 Web 核心 API 中的方法。但是数组以空值形式到达。

importProperties = function (url, callback, data) {
        var properties = JSON.stringify(data);
        $.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: { "": properties }
        })
            .done(callback)
            .fail(errorMessage);
    }

一旦数据被 JSON.stringified,它看起来像;

[{"UPRN":"12345","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B12345","PropertyNo":"1","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":1,"NumberOfBedrooms":2,"NumberOfKitchens":1,"PropertyContractId":0,"PropertyType":null},
{"UPRN":"67890","StreetName":"COMPORT GREEN","AddressLine2":"NEW ADDINGTON","AddressLine3":"Croydon","AddressLine4":"Surrey","Postcode":"CR0 0BY","Latitude":null,"Longitude":null,"BlockUPRN":"B67890","PropertyNo":"2","BlockName":"Block Court","Comments":null,"PropertyTypeId":6,"NumberOfBathrooms":null,"NumberOfBedrooms":null,"NumberOfKitchens":null,"PropertyContractId":0,"PropertyType":null}]

我的网络核心 API 上的方法签名是;

[HttpPost("{contractId}/import")]
public async Task<IActionResult> Import(int contractId, [FromBody] IEnumerable<PropertyAndContractDto> properties)
{
   this.NLogger.Info($"api/property/contractId = {contractId}/saveproperties".ToPrefix());
   if (properties == null)
   {
      return BadRequest();
   }
...
}

DTO 是

    public class PropertyAndContractDto
    {
        public string UPRN { get; set; }
        public string StreetName { get; set; }
        public string AddressLine2 { get; set; }
        public string AddressLine3 { get; set; }
        public string AddressLine4 { get; set; }
        public string Postcode { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string BlockUPRN { get; set; }
        public string PropertyNo { get; set; }
        public string BlockName { get; set; }
        public string Comments { get; set; }
        public int? PropertyTypeId { get; set; }
        public int? NumberOfBathrooms { get; set; }
        public int? NumberOfBedrooms { get; set; }
        public int? NumberOfKitchens { get; set; }
        public int PropertyContractId { get; set; }
        public string PropertyType { get; set; }
    }
}

当我在 Postman 中运行时,它可以工作;

那么为什么properties参数是null呢?

【问题讨论】:

  • 很可能是因为它无法解析为PropertyAndContractDto。发布 DTO 类。类属性是否与字符串中的相同? 你能把这个字符串解析成一个DTO数组吗?
  • 嗯,我猜模型绑定不起作用。您需要一个具有IEnumerable&lt;PropertyAndContractDto&gt; 类型属性的类。但不是 100% 确定。
  • 我已经发布了 Dto。对于 web 核心 api 和数据来说,它是同一个类。
  • 您是否尝试将data: { "": properties } 更改为data: { "properties": properties }
  • @Sebastian - 试过了,但我得到了同样的结果

标签: c# arrays json ajax asp.net-core-webapi


【解决方案1】:

尝试将 ajax 调用的 processData 属性设置为 false,如下所示:

$.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: { "": properties },
            processData: false
        })

根据docs

默认情况下,数据作为对象传入 data 选项 (从技术上讲,除字符串之外的任何内容)都将被处理并 转换为查询字符串,适合默认的内容类型 “应用程序/x-www-form-urlencoded”。如果你想发送一个 DOMDocument 或其他未处理的数据,将此选项设置为 false。

或者,只需将 data 属性设置为您的字符串化 json:

$.ajax({
            url: url,
            type: "POST",
            contentType: "application/json",
            dataType: "json",
            data: properties
        })

【讨论】:

  • 您的第二个建议就是答案。我以为我尝试了所有替代方案,但经过多次更正后,我没有恢复到这个。所以谢谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
相关资源
最近更新 更多