【问题标题】:REST service doesn't return a tuple correctlyREST 服务未正确返回元组
【发布时间】:2015-06-05 13:19:56
【问题描述】:

我有 REST 服务,我想从中返回一个元组。元组将是 2 个列表。请参阅下面的代码。 GetRecords 方法返回元组,当我检查结果变量时,我可以看到我需要的所有内容都在那里并且正确。

    public HttpResponseMessage GetRecords([FromUri] List<int> ids)
    {
        try
        {
            Tuple<List<Class1>, List<Class2>> result = DataAccess.GetRecords(ids);

            return Request.CreateResponse(HttpStatusCode.OK, result);
        }
    }

这是我的调用代码:

HttpResponseMessage response = await client.GetAsync(url);

Tuple<List<Class1>, List<Class2>> result = await response.Content.ReadAsAsync<Tuple<List<Class1>, List<Class2>>>();

所以一切似乎都可以正常工作,但是调用代码中的结果变量为空。有人知道我错过了什么吗?

如果我更改我的代码以返回一个列表而不是一个元组,那么它就会起作用。

请帮忙。

【问题讨论】:

  • JSON 是什么样的?

标签: c# rest tuples dotnet-httpclient


【解决方案1】:

根据MSDN,我假设您无法序列化元组,因为它缺少无参数构造函数。

编辑:

可以找到可能的解决方法there

【讨论】:

  • 谢谢。链接中的解决方法对我来说很好。
猜你喜欢
  • 1970-01-01
  • 2017-06-17
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多