json patch support the operation, a total of six kinds of
add: that is, or add an element to the array which add value to the resources inside.
Start writing code
JsonPatchDocument we need to install a database
mapping from employee to UpdateDto
example patchDocument if there is a read-only operations, which add a property that does not exist in C # class which, this time it will error, or is it you want to modify a read-only attribute. These will be given. So what we need to deal with this.
A replacement operation replace. The field is then replaced employeeNo. The replacement value is 1,111,122,222
422 error
because asp.net core 3.0 start he is using the json library is a relatively new json library. New library implementations of some of the more core functions. A lot of features not implemented.
Here we are to be replaced with json.net library.
204 again sends a request
query results EmployeeNo indeed been changed. But there is a problem. Query results become xml format of the
headers plus the type of reception.
But why did not write Accept the default headers returned xml format is it?
Because by default, it comes with the core json library, but then we added a json.net replace it, so the default is to return the xml format priority.
We add this to the front addXml json.net of
this without also accept return of the xml format.
He changed his name to
another query indeed been updated.
datetime type remove operation will set to default values
Add+copy
Process validation error
We first value employeeNo field removed.
It returns a 500 error. That is, did not take part in model binding the Controller.
Why not go because this type is not our UpdateDto JsonPatchDocument type.
So below, we will need to manually verify
Ruoguo validation fails to return false, and an error message will be placed inside ModelState.
ValidationProblem method is realized ValidateProbleDetails standard
jsonPatchDocument which can make mistakes. For example, we want to remove a non-existent field
just pass a field
return 500 is wrong, because it is an error caused by the client, it should be 4 beginning.
If patchDocument there are any validation errors, it will become the property of ModelState false, and an error message will be on the inside.
The jsonPatch also pointed out the error inside
Why deal with the return of 400 rather than 422 errors.
ValidationProblem error message is returned inside the manual return,
before we customized the wrong format problemDetails of.
But we here ValidationProblem method does not use a startup configuration inside the ProblemDetails configuration. So it returns 422 instead of 400 bad request
and when this model binding, verify that it uses the following configuration.
Modify the format returned
Re-wrote ValidationProblem
we finally return to the startup configuration is the use of
sending the request again
Local update or add
First a new or UpdateDto then put the value of the patchDocument inside dto inside pass over the modelState do verification.
Finally, it returns employeeDto
test
Id deliberately into the 9999
return header inside
copy headers see if I can get this value
End