我需要通過一些JSON包括GeoJSON的對象,並把它deserialise這種模式:反序列化以GeoJSON在C#中的Web API 2控制器
public class GeoTag
{
public Guid ID { get; set; }
public DbGeography Geography { get; set; }
public string Tag { get; set; }
public Guid UserID { get; set; }
}
我使用.NET中的Web API 2控制器4.5如下:
[ResponseType(typeof(GeoTag))]
public async Task<IHttpActionResult> PostQuestion(GeoTag geoTag)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
//Do some stuff
return CreatedAtRoute("DefaultApi", new { id = geoTag.ID }, geoTag);
}
這是我的JSON:
{
"Tag": "food",
"Geography": {
"coordinates": ["-0.1337","51.50998"],
"type": "Point"
}
}
我無法獲得控制器方法interprate Geography
;它返回此消息:
Unable to find a constructor to use for type System.Data.Spatial.DbGeography. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Geography.coordinates', line 4, position 19.
有誰知道我做的事情是可能的或者也許我的方法是錯誤的?
您提供一個心不是有效的JSON JSON的樣品。 – Jerin
首先改變你的Json,這是無效的。 – KiShOrE
像下面的答案一樣改變你的Json。 – KiShOrE