The ‘ObjectContent`1’ type failed to serialize the response body for content type

Lỗi: The ‘ObjectContent`1’ type failed to serialize the response body …

Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8’.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type ‘System.Data.Entity.DynamicProxies.Note_F843D42272465FB5DCED8BD44BD094791BE3AEBE38A117FA4DA7420DB2525323’ with data contract name ‘Note_F843D42272465FB5DCED8BD44BD094791BE3AEBE38A117FA4DA7420DB2525323:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies&#8217; is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>

Xử lý: Chèn đoạn code này vào file WebApiConfig.cs trong phương thức public static void Register(HttpConfiguration config)

 var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s