Reputation: 12715
I have the following class definition
#nullable enable
namespace Test
{
public class MyDetails
{
public string? SomeName { get; set; }
}
}
example usage
[HttpGet]
public IActionResult GetDetails()
{
return Ok(new MyDetails());
}
which generates the following json:
{}
instead of
{
"someName" : null
}
why the null value makes the property to be removed from the output's json?
Upvotes: 1
Views: 135
Reputation: 1026
Try this:
services.AddControllers().AddJsonOptions(options => {
options.JsonSerializerOptions.IgnoreNullValues = false;
});
Upvotes: 3