C# MongoDB GeoJsonPoint JSON 对象
C# MongoDB GeoJsonPoint JSON Object
我遇到了如何使用 mongodb.driver
在我的 ASP.NET Core 3 应用程序中构造我的地理位置对象的问题
我的对象
public class Person
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
}
我只是在做一个直线插入并传递对象。如果我遗漏了它插入的位置。
这是我试图通过简单的 post.
传递给 api 的 JSON 对象
{
"firstName": "Paul",
"lastName": "Staley",
"email": "someemail@nowhere.com",
"address": "12345 E 1st Ave",
"state": "AA",
"zipCode": "11111",
"phoneNumber": "555-555-5555",
"location": {
"coordinates": [
0.123321,
0.3453455
]
}
}
我得到的错误。
System.NotSupportedException:不支持没有无参数构造函数的引用类型的反序列化。输入 'MongoDB.Driver.GeoJsonObjectModel.GeoJsonPoint`1[MongoDB.Driver.GeoJsonObjectModel.GeoJson2DGeographicCoordinates]'
Nksoi 帮助回答
人物对象
public class Person
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location
{ get; private set; }
public void SetLocation(double lon, double lat) => SetPosition(lon, lat);
private void SetPosition(double lon, double lat)
{
Latitude = lat;
Longitude = lon;
Location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(
new GeoJson2DGeographicCoordinates(lon, lat));
}
}
控制器
[HttpPost]
public IActionResult CreatePerson(Person person)
{
person.SetLocation(person.Longitude, person.Latitude);
_personRepository.CreatePerson(person);
return Ok();
}
JSON 对象
"firstName": "Paul",
"lastName": "Staley",
"email": "someemail@nowhere.com",
"address": "12345 E 1st Ave",
"state": "AA",
"zipCode": "11111",
"phoneNumber": "555-555-5555",
"latitude": 32.0,
"longitude": 91.0
我遇到了如何使用 mongodb.driver
在我的 ASP.NET Core 3 应用程序中构造我的地理位置对象的问题我的对象
public class Person
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }
}
我只是在做一个直线插入并传递对象。如果我遗漏了它插入的位置。
这是我试图通过简单的 post.
传递给 api 的 JSON 对象{
"firstName": "Paul",
"lastName": "Staley",
"email": "someemail@nowhere.com",
"address": "12345 E 1st Ave",
"state": "AA",
"zipCode": "11111",
"phoneNumber": "555-555-5555",
"location": {
"coordinates": [
0.123321,
0.3453455
]
}
}
我得到的错误。
System.NotSupportedException:不支持没有无参数构造函数的引用类型的反序列化。输入 'MongoDB.Driver.GeoJsonObjectModel.GeoJsonPoint`1[MongoDB.Driver.GeoJsonObjectModel.GeoJson2DGeographicCoordinates]'
Nksoi 帮助回答
人物对象
public class Person
{
[BsonId]
public ObjectId Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string PhoneNumber { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location
{ get; private set; }
public void SetLocation(double lon, double lat) => SetPosition(lon, lat);
private void SetPosition(double lon, double lat)
{
Latitude = lat;
Longitude = lon;
Location = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(
new GeoJson2DGeographicCoordinates(lon, lat));
}
}
控制器
[HttpPost]
public IActionResult CreatePerson(Person person)
{
person.SetLocation(person.Longitude, person.Latitude);
_personRepository.CreatePerson(person);
return Ok();
}
JSON 对象
"firstName": "Paul",
"lastName": "Staley",
"email": "someemail@nowhere.com",
"address": "12345 E 1st Ave",
"state": "AA",
"zipCode": "11111",
"phoneNumber": "555-555-5555",
"latitude": 32.0,
"longitude": 91.0