[FromBody]JToken 参数在 ContentType: application/json 时为空,或者在 ContentType: application/x-www-form-urlencoded [Fiddler] 时序列化错误

[FromBody]JToken param is null when ContentType: application/json or it's bad serialized when Contenttype: application/x-www-form-urlencoded [Fiddler]

如主题所述,我在尝试通过 Fiddler Body 请求调用端点 (POST) 时遇到问题。

有一个 http api 方法:

[HttpPost]
[Route("books")]
 public IHttpActionResult GetBooks([FromBody]JToken filterRequest)
 {...}

在 Fiddler 中,我进行了 POST 调用,请求 header:

User-Agent: Fiddler Host: localhost Content-Length: 21 Content-Type: application/json

请求 Body 看起来像:{ "title" : "Harry Potter"}。在调试它时,我得到 null filterRequest 参数。

但是当我使用 Content-Type: application/x-www-form-urlencoded 时,参数 filterRequest 参数 不为空但不幸的是包含不正确的语法,例如:

{{"{ \"title\" : \"Harry Potter\"}": ""}}

错误的序列化者:

var columnsFilter = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(filter);

我不知道我在这里做错了什么。也许您遇到过类似的问题并可以提供帮助?

原来整个问题都是基于错误的引用。我只需要使用 ' 并且发布的请求正文是正确的 Content-Type: application/json.