ASP.NET Core 3.0 [FromBody] 字符串内容返回“无法将JSON值转换为System.String。”

64
在 ASP.NET Core 3.0 中,在 ApiController 上使用 [FromBody] 字符串内容会返回验证错误。
{"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
 "title":"One or more validation errors occurred.",
 "status":400,
 "traceId":"|9dd96d96-4e64bafba4ba0245.",
 "errors":{"$":["The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 1."]}}

当客户端以内容类型:application/json 发送数据时,我该如何在.NET Core 3.0的API控制器中获取原始的JSON数据字符串?而无需客户端更新其内容类型。

1
这个在 .net core 2.1 中能用吗?请发一下你的 JSON 示例。 - cdev
1
我决定使用StreamReader并自己读取Request.Body。这是一个新项目,尚未在2.1上进行测试,但过去可能已将body绑定到JToken而不是字符串。 - Poul K. Sørensen
11个回答

-2
你需要将Json对象转换为字符串,然后发送到服务器。就像JSON.stringify(jsonObj)一样。

3
我无法更换客户。 - Poul K. Sørensen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接