I've used scaffolding to generate a Controller class for Web API 2 in ASP.NET.
I'd like to post an object from a browser. This is the generated method running:
// POST: api/MyObjects
[ResponseType(typeof(MyObject))]
public async Task<IHttpActionResult> PostMyObject(MyObject myObject)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.MyObjects.Add(myObject);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (MyObjectExists(myObject.Id))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = myObject.Id }, myObject);
}
I'm trying to invoke it from javascript/jquery:
$.post("http://localhost:2239/api/UrlVisits/5", "=" + "something");
My question is, what do I put in for "something" to serialize it correctly? When the method is entered, myObject is a valid object with all fields set to null. I don't know where in the stack this serialization is happening, so I'm not sure how to modify my post to fix it.