I am using Angularjs $Resource $save CRUD method to submit data to RESTFull .Net Web API webservice:
phone.$save({}, function (data) {
alert('success');
}, function (data) {
alert('failed');
});
Where phone is a $resource object. The call successfully passes the phone object to the Web API:
public async Task<Phone> Post([FromBody]Phone phone)
{
try
{
if (ModelState.IsValid)
{
phone.CreatedDate = DateTime.Now;
this.Uow.Phones.Add(phone);
this.Uow.Commit();
return phone;
}
}
catch (Exception ex)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError));
}
return null;
}
but when returning the response the failed callback is triggered with the following error:
"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'."
In Angularjs documentation for the $save function we have:
card.$save();
// POST: /user/123/card/456 {id:456, number:'1234', name:'J. Smith'}
// server returns: {id:456, number:'1234', name: 'J. Smith'};
Which means that the response should return the same object but with updated fields.
How can a valid response be returned from the Web Api?
Thanks!