I have several Classes generated by the entity framework and they need to be serialized like this;
string OldVersion = JsonConvert.SerializeObject(foo);
but I get the message:
Self referencing loop detected with type 'System.Data.Entity.DynamicProxies
I have consulted and found that there must be a WebApiConfig class that does not exist in the project that I am modifying, and place the following lines of code to avoid the error
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Is there another way to add those lines directly in the web.config or another way to avoid this error?
I use Newtonsoft.json
It uses the method overload
SerializeObject
to tell it the serialization options.It would be something like this:
If you want to do it globally (that is, for the entire application), you can configure it in the method
Application_Start()
of theGlobal.asax
.It would be something like this: