【发布时间】:2020-01-14 19:18:25
【问题描述】:
我正在尝试在 .NET Core 3.0 Preview 9 web api 上实现 JsonPatch。
型号:
public class TestPatch
{
public string TestPath { get; set; }
}
web api 端点:
[HttpPatch()]
public async Task<IActionResult> Update([FromBody] JsonPatchDocument<TestPatch> patch)
{
...........
return Ok();
}
JSON 负载:
[
{
"op" : "replace",
"path" : "/testPath",
"value" : "new value"
}
]
通过 Postman 使用 PATCH,我收到此错误:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|492c592-4f7de4d16a32b942.",
"errors": {
"$": [
"The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[Test.Models.TestPatch]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."
]
}
}
这是来自 Postman 的完整请求/响应
PATCH /api/helptemplates HTTP/1.1
Content-Type: application/json
User-Agent: PostmanRuntime/7.16.3
Accept: */*
Cache-Control: no-cache
Postman-Token: a41813ea-14db-4664-98fb-ee30511707bc
Host: localhost:5002
Accept-Encoding: gzip, deflate
Content-Length: 77
Connection: keep-alive
[
{
"op" : "replace",
"path" : "/testPath",
"value" : "new value"
}
]
HTTP/1.1 400 Bad Request
Date: Thu, 12 Sep 2019 21:13:08 GMT
Content-Type: application/problem+json; charset=utf-8
Server: Kestrel
Transfer-Encoding: chunked
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|492c593-4f7de4d16a32b942.","errors":{"$":["The JSON value could not be converted to Microsoft.AspNetCore.JsonPatch.JsonPatchDocument`1[Test.Models.TestPatch]. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}
JsonPatch 参考:
<PackageReference Include="Microsoft.AspNetCore.JsonPatch" Version="3.0.0-preview9.19424.4" />
我的代码有什么问题?
谢谢。
【问题讨论】:
-
在github.com/aspnet/AspNetCore/issues/13938 发布了一个问题,并已通过使用 Microsoft.AspNetCore.Mvc.NewtonsoftJson 解决。
标签: asp.net .net-core .net-core-3.0 asp.net-core-3.0