I just downloaded the ASP.NET MVC4 Beta to look into creating a Web API project to return some of my entities classes. I coded a simple Get method to just to see how things would work:
publicIEnumerable<MvcAppiTester.esApplication> Get()
{
PSASecurityEntities db = newPSASecurityEntities();
var apps = from a in db.esApplications select a;
return apps;
}
The type 'MvcAppiTester.esApplication' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references.
To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
publicIEnumerable<MvcAppiTester.esApplication> Get()
{
PSASecurityEntities db = newPSASecurityEntities();
var apps = from a in db.esApplications select a;
return Json(apps);
}
Isnt the whole point of WebAPI to send content based upon content-accept, so if a client want xml the API return XML, forcing JSON is not a good solution.
i've come across this before and wrapped my returning object(s) in an custom object...
MyContext myContext = new MyContext ();
var ents = from d in myContext.MyEntities
select d;
// Pack the entities into our POCO.
var newEnts = from d in ents
select new MyPoco
{
PocoId = SqlFunctions.StringConvert(d.EntId),
PocoName = d.Name
};
return newEnts;
Have you tried disabling IsReference on the type 'MvcAppiTester.esApplication'?
[DataContract(IsReference=false)]
If you don't have control over this type, then it seems like the default JSON serializer DataContractJsonSerializer is unable to serialized this type. A way to overcome this is to create a custom formatter that uses Json.NET -- Henrik has a good blog post
(with sample code) on such JsonNetFormatter: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
Thanks to everyone for all of their ideas. In the end, I had to create a new default media formatter. I got most of the code from https://github.com/ChristianWeyer/Thinktecture.Web.Http With some guidance from others. Things are working well now. -- phantasm33
phantasm33
Member
3 Points
2 Posts
Error Returning Entities As JSON Objects
Feb 23, 2012 03:08 AM|LINK
I just downloaded the ASP.NET MVC4 Beta to look into creating a Web API project to return some of my entities classes. I coded a simple Get method to just to see how things would work:
The type 'MvcAppiTester.esApplication' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
How do overcome this limitation?
vinay13mar
Star
7756 Points
1626 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 03:32 AM|LINK
Hi,
check the modified code below in bold
V.K.Singh
aspnet2sams
Participant
1746 Points
541 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 04:17 AM|LINK
Hi phantasm33,
coincidently working on similar task. I tried to get using:
public JsonResult Get()
{
return Json(object);
}
Not sure if it works for you.
Please let me know more of your code if possible.
aspnet2sams
Participant
1746 Points
541 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 04:57 AM|LINK
Hi experts,
My code snippet:
public JsonResult Getapidata() { object file = Server.MapPath("~/<wesite_name>/<page>"); return Json(file,JsonRequestBehavior.AllowGet); }HSaid
Member
2 Points
9 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 04:59 AM|LINK
Isnt the whole point of WebAPI to send content based upon content-accept, so if a client want xml the API return XML, forcing JSON is not a good solution.
aspnet2sams
Participant
1746 Points
541 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 05:11 AM|LINK
what client are u referring too? if browser of machine, let me inform you mine is in development stage.
"forcing JSON is not a good solution", I agree but, yet trying with possible solutions.
WindKnot
Member
24 Points
10 Posts
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 06:14 AM|LINK
i've come across this before and wrapped my returning object(s) in an custom object...
MyContext myContext = new MyContext (); var ents = from d in myContext.MyEntities select d; // Pack the entities into our POCO. var newEnts = from d in ents select new MyPoco { PocoId = SqlFunctions.StringConvert(d.EntId), PocoName = d.Name }; return newEnts;maggie.ying
Member
183 Points
39 Posts
Microsoft
Re: Error Returning Entities As JSON Objects
Feb 23, 2012 07:07 AM|LINK
Have you tried disabling IsReference on the type 'MvcAppiTester.esApplication'?
If you don't have control over this type, then it seems like the default JSON serializer DataContractJsonSerializer is unable to serialized this type. A way to overcome this is to create a custom formatter that uses Json.NET -- Henrik has a good blog post (with sample code) on such JsonNetFormatter: http://blogs.msdn.com/b/henrikn/archive/2012/02/18/using-json-net-with-asp-net-web-api.aspx
Thanks,
Maggie Ying
phantasm33
Member
3 Points
2 Posts
Re: Error Returning Entities As JSON Objects
Mar 01, 2012 12:50 AM|LINK