I have been struggling with my first c# project. This is a web api controller that will receive a post from AngularJS. The post is coming across but appears to have an extra set of brackets in the JSON object (right click/copy value in vs 2015) . I have tried various methods but everytime I deserialize the JSON object I get null values.
public class LocationsTestController : ApiController
{
[System.Web.Http.HttpPost]
[Route("")]
public IHttpActionResult Post(object json)
{
string sjson = json.ToString();
Coords oCoords = JsonConvert.DeserializeObject<Coords>(sjson);
DBEntities db = new DBEntities();
db.spUpdateLocation(User.Identity.Name, oCoords.latitude.ToString(), oCoords.longitude.ToString());
db.SaveChanges();
return Ok(oCoords.latitude); //return trash data for now
}
}
Here is my JSON copied from the the object received by the post.
{{
"coords": {
"latitude": 43.445969749565833,
"longitude": -80.484091512936885,
"altitude": 100,
"accuracy": 150,
"altitudeAccuracy": 80,
"heading": 38,
"speed": 25
},
"timestamp": 1442113213418
}}
I have tried mapping to the entity framework, but problem is I need to add the authenticated username to the json object as I don't want to trust what is being passed to the API.
Any help would be much appreciated.