繁体   English   中英

在 C# 中的 Web API 2 控制器中反序列化 geoJSON

[英]Deserializing geoJSON in a Web API 2 controller in c#

我需要传递一些包含 geoJSON 对象的 JSON 并将其反序列化为此模型:

public class GeoTag
    {
        public Guid ID { get; set; }
        public DbGeography Geography { get; set; }
        public string Tag { get; set; }
        public Guid UserID { get; set; }
    }

我在 .NET 4.5 中使用 Web API 2 控制器,如下所示:

[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"
}

}

我在获取控制器方法来解释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,

{ "Tag": "food", "Geography": { "coordinates": ["-0.1337","51.50998"], "type": "Point" } }

它会工作.. :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM