C#C
C#3y ago
cumslvt13

✅ Binding array with default value doesn't work

I've got the following model and controller
public class MyModel
{
    public int? Test { get; set; } = 10;
    public IEnumerable<string> Sections { get; set; } = Array.Empty<string>();
}

[ApiController]
[Route("[controller]")]
public class TestController : ControllerBase
{
    [HttpGet]
    public ActionResult<MyModel> Test([FromQuery] MyModel model)
    {
        return Ok(model);
    }
}

The https://localhost:5001/test?Test=3&Sections=USER resolves to {"test":3,"sections":[]}, but if I remove default value for a Sections property it resolves to {"test":3,"sections":["USER"]}. Why behavior is different from int default value?
Was this page helpful?