A few weeks ago I blogged about an issue where my breakpoint inside an ASP.NET MVC controller was never hit. The problem was caused by the built-in ModelStateInvalidFilter.
What I didn’t share in that blog post is why the ModelState was invalid in the first place. Let’s see how a small change in my csproj file caused this…
This is the model I was using:
And this was an example I tried to send through my API:
Everything worked until I made one small change. I updated my csproj file to start using nullable reference types:
Now when I executed the same code it failed?! One look at the ModelState errors explained it:
The FromAddress field is required.
By enabling nullable reference types it handled both the ‘ToAddress’ and ‘FromAddress’ as required.
To fix it, I had to update the DTO and enable nullability: