.Net Framework Web API - Custom Json serialization for requests and responses

397 views Asked by At

I am trying to find an example of how to customize my .Net Framework Web API, to handle requests and responses with a custom serialization library instead of the built-in Newtonsoft library. So when request arrives to one of my controllers with content-type of 'application/json', my custom formatter will automatically deserialize its body to the controller's expected object. And when an object is returned from an of controller's endpoints - again, my custom formatter will take this object and serialize it to json string.

1

There are 1 answers

0
user2055886 On BEST ANSWER

Ok, Found the solution. Basically I just needed to implement System.Net.Http.Formatting.MediaTypeFormatter abstract class. This implementation will be used as definition for my API endpoints serialization. Eventually I needed to add my new implementation to GlobalConfiguration object in Global.asax file:

GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new MyNewFormatter());