In ASP .NET Core, given a controller method for a PATCH endpoint like this:
[HttpPatch]
[Route("{entity_id}")]
public async Task<IActionResult> UpdateEntity(
[FromRoute(Name = "entity_id")]int entity_id,
[FromBody(Name = "")]EntityUpdate entityUpdate,
CancellationToken cancellationToken
)
{
await UpdateEntity(
entity_id,
entityUpdate,
cancellationToken
);
return Ok();
}
And the following definition for EntityUpdate
:
[DataContract(Name = "entity_update")]
public class EntityUpdate
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "description")]
public string Description { get; set; }
}
Is there anyway to tell if a given property of EntityUpdate
was explicitly set by the client to null
or simply omitted?
I forgo the autoproperty and include a Changed
property.
[DataContract(Name = "entity_update")]
public class EntityUpdate
{
private string _name;
private bool _nameChanged;
[DataMember(Name = "name")]
public string Name { get => _name; set { _name = value; _nameChanged = true; } }
[DataMember(Name = "description")]
public string Description { get; set; }
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加