ReSTful Web Service : Enable JSON Formatting

This tutorial is related to development of ReSTful web service using Microsoft Visual Studio for ASP.NET 4.5.2 template for Web API project.

Exception Error Message:

This XML file does not appear to have any style information associated with it. The document tree is shown below.

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



Solution:

https://stackoverflow.com/questions/23098191/failed-to-serialize-the-response-in-web-api-with-json

Add the following code (c#) to global.asax under Application_Start method :

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
    .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters
    .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);  


Scenario:

To simulate this exception error, follow the instructions in the tutorial below by starting a new project:

https://www.codeproject.com/Tips/1136105/Web-API-Return-List-of-Custom-Type-JSON

Comments