c# - The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8' -
for months following web api 2 (simplified example) responded correctly to
http://localhost/testapi2/api/tests this. [{"id":1,"name":"naples"},{"id":2,"name":"paris"}] //using newtonsoft.json; namespace testapi2.controllers { public class personscontroller : apicontroller { [httpget] [route("api/tests")] public httpresponsemessage getall() { // dbcontext generated databasefirst ef generation using (var cleandbcontext = new cleandbentities()) { var tests = cleandbcontext.testtables; // return return request.createresponse(httpstatuscode.ok, tests, configuration.formatters.jsonformatter); } } } }
then updated nugets on project same call responded with:
<exceptionmessage> 'objectcontent`1' type failed serialize response body content type 'application/json; charset=utf-8'. </exceptionmessage> <exceptiontype>system.invalidoperationexception</exceptiontype> <stacktrace/>
apparently in nuget update, perhaps newtonsoft, formatter, cannot serialize list implied in "var list =". list ok because can foreach through examine contents.
after lot of agony, , experimentation, changed query explicitly generate list while keeping anonymous var target.
var tests = cleandbcontext.testtables.tolist();
modifying query explicitly generate list fixed issue. i'll send info newtonsoft. it's great product.
Comments
Post a Comment