5

当我像这样在 RestSharp 中提出请求时:

var response = client.Execute<bool>(request);

我收到以下错误:

"Unable to cast object of type 'System.Boolean' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'."

这是完整的 HTTP 响应,每个 Fiddler:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Apr 2013 15:09:14 GMT
Content-Length: 5

false

似乎所有的反应都符合犹太教规,那又是什么呢?

另外,如果我通过返回一个简单的值而不是一个对象来对我的 WebAPI 控制器做一些愚蠢的事情并且可以解决我的问题,请随时提出建议。

4

1 回答 1

9

RestSharp 只会反序列化有效的 json。false不是有效的 json(根据 RFC-4627)。服务器至少需要返回如下内容:

{ "foo": false }

你需要一个像下面这样的类来反序列化:

public class BooleanResponse
{
    public bool Foo { get; set; }
}
于 2013-04-01T18:29:44.560 回答