# Microsoft.AspNetCore.JsonPatch.JsonPatchException: 路径 'path' 处的属性无法更新。

- **ID:** `dotnet/aspnetcore-json-patch-document-invalid`
- **领域:** dotnet
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

JSON Patch 操作（如 replace、add、remove）针对目标对象上不存在的属性，或操作违反了模型验证规则，通常是由于路径语法错误或缺少属性。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 6.0 | active | — | — |
| 7.0 | active | — | — |
| 8.0 | active | — | — |

## 解决方案

1. ```
   Validate the JSON Patch document before applying it: use 'var patchDoc = new JsonPatchDocument<MyModel>(); patchDoc.Operations.Add(new Operation("replace", "/PropertyName", null, "newValue"));' and check that '/PropertyName' exists via reflection or model metadata.
   ```
2. ```
   Apply the patch to a copy of the model and use TryValidateModel to catch validation errors: 'var tempModel = new MyModel(); patchDoc.ApplyTo(tempModel); if (!TryValidateModel(tempModel)) return BadRequest(ModelState);'
   ```

## 无效尝试

- **** — Adding [FromBody] to the action parameter does not fix the patch document; the issue is with the operation path or target model structure. (90% 失败率)
- **** — Changing the HTTP method from PATCH to PUT may bypass the patch logic but breaks RESTful conventions and can cause unintended full replacement of the resource. (70% 失败率)
