.NET Core: Remove null fields from API JSON response

后端 未结 6 1441
无人共我
无人共我 2020-12-08 03:58

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

6条回答
  •  無奈伤痛
    2020-12-08 04:12

    This can also be done per controller in case you don't want to modify the global behavior:

    public IActionResult GetSomething()
    {
       var myObject = GetMyObject();
       return new JsonResult(myObject, new JsonSerializerSettings() 
       { 
           NullValueHandling = NullValueHandling.Ignore 
       });
    };
    

提交回复
热议问题