c# - 无法将 Json 数组发布到 Web 核心 API

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

通过这段代码,我试图将一个 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 中运行时,它起作用了;

enter image description here

那么为什么 properties 参数为 null?

最佳答案

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

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

根据docs :

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

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

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

关于c# - 无法将 Json 数组发布到 Web 核心 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728774/

相关文章:

php - 使用 usort 和全局变量对数组进行排序

json - 当我尝试在 spark 上解析 Json 时出现 java.lang.NoSuchMethodError

json - 如何在 Log-stash HTTP 输出中映射嵌套的 JSON

c# - 如何 bundle 在 MVC4 中的某些 View 之间共享的 css

c# - 如何从 Linq 读取大型 XML RAW 到 SQL?

arrays - 生成日期时间范围

c++ - 具有固定大小数组的 STL 算法的效率

html - Excel 到 HTML 表格

c# - 如何为lambda表达式中的匿名变量编写IEqualityComparer?

c# - 在 XAML 中将 bool 值返回 null 到复选框状态转换器