ASP.Net MVC, EntityFramework, MVC WebAPI

Runtime error on REST WebAPI : failed to serialize the response body for content type ‘application/xml; charset=utf-8

In a Relational Database system , we may get this type of runtime error when calling WebAPI for a composite Object .

The exception looks like this :::

The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8

We can solve this problem by adding the below lines in WebApiConfig.cs file under App_Start folder inside WebAPI project .

var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;

Will write few full step by step articles on EntityFramework and WebAPI on this blog , so Please Keep an eye on this Blog .

Thanks 🙂

Standard

One thought on “Runtime error on REST WebAPI : failed to serialize the response body for content type ‘application/xml; charset=utf-8

  1. vick says:

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

    without adding the last line it wont work in my case.. although it works without all the above code when called the webapi using POSTMAN rest client.

Leave a Reply

Your email address will not be published. Required fields are marked *