2015-06-29 34 views
-1

我需要通過一些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. 

有誰知道我做的事情是可能的或者也許我的方法是錯誤的?

+0

您提供一個心不是有效的JSON JSON的樣品。 – Jerin

+0

首先改變你的Json,這是無效的。 – KiShOrE

+0

像下面的答案一樣改變你的Json。 – KiShOrE

回答

0

更改您的JSON這樣,

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

它將工作.. :)

+0

謝謝@KiShOrE。我改變了JSON,但沒有喜悅。我用你的JSON更新了這個問題,並且包含了從我的web方法返回的消息。有任何想法嗎? –

+0

更新後得到的錯誤是什麼。 – KiShOrE

+0

我已經添加了回到上面的問題的消息:開始'無法找到一個構造函數用於類型System.Data.Spatial.DbGeography' –