Suppress properties with null value on ASP.NET Web API

In the WebApiConfig:

config.Formatters.JsonFormatter.SerializerSettings = 
                 new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore};

Or, if you want more control, you can replace entire formatter:

var jsonformatter = new JsonMediaTypeFormatter
{
    SerializerSettings =
    {
        NullValueHandling = NullValueHandling.Ignore
    }
};

config.Formatters.RemoveAt(0);
config.Formatters.Insert(0, jsonformatter);

Leave a Comment