Post发送geojson数据失败,.NET Core后端报错System.ArgumentNullException: Value cannot be null. (Parameter 'request')

问题描述

前端使用$.ajax发送数据,后端报错Controller的入参为null

$.ajax({
                url: "/test",
                data: JSON.stringify({
                    Entity: params
                }),
                type: "POST",
                dataType: "json",
                async: false,
                contentType: "application/json",
})

其中params包含一个属性,值是geojson对象组成的数组,类似于

[
  {
    "type": "Polygon",
    "coordinates": [
      [
        [100.0, 0.0],
        [101.0, 0.0],
        [101.0, 1.0],
        [100.0, 1.0],
        [100.0, 0.0]
      ]
    ]
  },{
    "type": "Polygon",
    "coordinates": [
      [
        [100.1, 0.1],
        [101.0, 0.0],
        [101.0, 1.0],
        [100.0, 1.0],
        [100.1, 0.1]
      ]
    ]
  }
]

之前Post请求都能正常发送,但突然遇到上面说的问题

经过排查,问题出在geojson对象的bbox属性上

 geojson的原型链上存在属性bbox,在序列化时被一起写进了json中

后端在接受到请求后,会进行模型绑定,矢量数组在后端的类型是

public IList<NetTopologySuite.Geometries.Geometry> Shapes { get; set; }

如果有bbox,模型绑定有时候就会失败报错

JSON input formatter threw an exception: The JSON value could not be converted to NetTopologySuite.Geometries.Geometry. Path: $.Entity.shapes[0] | LineNumber: 0 | BytePositionInLine: 6537.

 

解决办法

前端构造参数时,只保留geojson中需要的字段

const { type, coordinates } = Terraformer.ArcGIS.parse(graphic.geometry);
shapes.push({ type, coordinates });

 

posted @ 2024-06-05 15:13  UTA_RED  阅读(83)  评论(0)    收藏  举报