On a global level in .NET Core 1.0 (all API responses), how can I configure Startup.cs so that null fields are removed/ignored in JSON responses?
Using Newtonsoft.Js
[.NET Core 1.0]
In Startup.cs, you can attach JsonOptions to the service collection and set various configurations, including removing null values, there:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
}
[.NET Core 3.1]
Instead of:
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
Use:
options.JsonSerializerOptions.IgnoreNullValues = true;