In some cases, you may have an object the you are returning in an System.Web.Http.IHttpActionResult response where properties in the object you are returning have null values. To minimize the size of the data structure you are returning, a formatter can be specified which will let you supress null values.
Add using System.Net.Http.Formatting to the top of your code file.
protected JsonMediaTypeFormatter getJsonFormatter()
{
JsonMediaTypeFormatter formatter = new JsonMediaTypeFormatter
{
SerializerSettings =
{
NullValueHandling = NullValueHandling.Ignore
}
};
return formatter;
}Lets say you have a list of interesting things called model_list which you load into a response object called ResponseDetailModel and you want to return this as JSON in a HTTP OK response, you can make the following call.
return Content(HttpStatusCode.OK, new ResponseDetailsModel(model_list), getJsonFormatter());
Like magic, your client will receive json with "content-type": "application/json; charset=utf-8" in the header that has no null properties.