Tony
Tony

Reputation: 12715

ASP.NET Core 3 - nullable type - removes property from json

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

Answers (1)

Arsalan Valoojerdi
Arsalan Valoojerdi

Reputation: 1026

Try this:

services.AddControllers().AddJsonOptions(options => {
     options.JsonSerializerOptions.IgnoreNullValues = false;
});

Upvotes: 3

Related Questions