我在 Microsoft 文档和他们调用 JsonPatchDocument.ApplyTo(patchObject, ModelState) 的许多示例中看到,但我似乎无法让它以这种方式构建。我收到一个构建错误,提示“错误 CS1503 参数 2:无法从 'System.Web.Http.ModelBinding.ModelStateDictionary' 转换为 'System.Action'”
我引用的是 Microsoft.AspNetCore.JsonPatch v2.2.0,示例代码来自这里:
https://docs.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-2.2
[HttpPatch]
public IActionResult JsonPatchWithModelState(
[FromBody] JsonPatchDocument<Customer> patchDoc)
{
if (patchDoc != null)
{
var customer = CreateCustomer();
patchDoc.ApplyTo(customer, ModelState);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return new ObjectResult(customer);
}
else
{
return BadRequest(ModelState);
}
}