This is my very first question after many years of lurking here, so I hope I don't break any rules.
In some of my ASP.NET Core API's POST methods, I'd like to make it possible for clients to provide only the properties they want to update in the body of their POST request.
Here's a simplified version of my code:
[Route("api/v{version:apiVersion}/[controller]")]
[ApiController]
public sealed class FooController : ControllerBase
{
public async Task<IActionResult> UpdateFooAsync(Guid fooGuid, [FromBody]UpdateFooModel model)
{
... Apply updates for specified properties, checking for authorization where needed...
return Ok();
}
}
public sealed class UpdateFooModel
{
[BindProperty] public int? MaxFoo { get; set; }
[BindProperty] public int? MaxBar { get; set; }
}
public sealed class Foo
{
public int? MaxFoo { get; set; }
public int? MaxBar { get; set; }
}
MaxBar and MaxFoo both are nullable integer values, where the null value signifies there's no maximum.
I'm trying to make it possible to let clients send e.g. the following to this endpoint:
Setting MaxBar to null, and setting MaxFoo to 10
{ "maxBar": null, "maxFoo": 10 }
Setting MaxBar to null, not touching MaxFoo
{ "maxBar": null }
Update MaxBar to 5, not touching MaxFoo
{ "maxBar": 5 }
In my method UpdateFooAsync, I want to update only the properties that have been specified in the request.
However, when model binding occurs, unspecified properties are set to their default values (null
for nullable types).
What would be the best way to find out if a value was explicitly set to null
(it should be set to null
), or was just not present in the request (it should not be updated)?
I've tried checking the ModelState
, but it contained no keys for the 'model', only for the Guid
typed parameter.
Any other way to solve the core problem would be welcome as well, of course.
Thanks!
checking for authorization where needed
part in my example code: Not every user will be authorized to update all properties It also is slightly more complex for the client, while I only want to support 'replace' operations. – Airglow