I see in the Microsoft docs and many examples where they call JsonPatchDocument.ApplyTo(patchObject, ModelState), but I can't seem to get it to build that way. I get a build error saying "Error CS1503 Argument 2: cannot convert from 'System.Web.Http.ModelBinding.ModelStateDictionary' to 'System.Action'"
I'm referencing Microsoft.AspNetCore.JsonPatch v2.2.0 and the sample code is from here:
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);
}
}
You have to add Newtonsoft package for .Net Core
Microsoft.AspNetCore.Mvc.NewtonsoftJson
dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson
You have to install this NuGet package:
Microsoft.AspNetCore.Mvc.NewtonsoftJson
You also have to add these method calls to the ConfigureServices method in the Startup class:
services
.AddControllersWithViews()
.AddNewtonsoftJson();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With