RestSharp StatusCode vs IsSuccessful

时间:2017-12-29 07:27:40

标签: c# .net restsharp

我有以下课程

public class Customer
    {
        public Customer()
        {
            Project = new HashSet<Project>();
        }

        public uint Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }

        public HashSet<Project> Project { get; set; }
    }

以下方法

protected async Task<IRestResponse> ApplyRequest<T>(string resource, Method method, T data) where T : class, new()
        {
            var client = new RestClient(_connectionConfig.BaseUrl);
            client.FollowRedirects = false;
            var request = new RestRequest(resource, method);
            request.RequestFormat = RestSharp.DataFormat.Json;
            request.AddParameter("application/json", JsonConvert.SerializeObject(data), ParameterType.RequestBody);
            //request.AddJsonBody(data); //This also doesn't work
            var response2 = await client.ExecuteTaskAsync<T>(request);
            return response2;
        }

现在,如果我使用Post方法调用此方法,则返回状态代码为&#34;已创建&#34; (它实际上是创建的)。但是,IsSuccessful属性给出了&#34; false&#34;并且错误消息是

  

&#34;无法投射类型&#39; SimpleJson.JsonArray&#39;输入   &#39; System.Collections.Generic.IDictionary`2 [System.String,System.Object的]&#39;&#34;

这是通常的,还是我做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

它不成功,因为您遇到了序列化错误。我还看到您在GiTHub上记录了它,并得到了相同的响应。 https://github.com/restsharp/RestSharp/issues/1064