【问题标题】:.Net HttpResponseMessage.Content.ReadAsAsync<Result<TResponse>> getting null always in the TResponse value.Net HttpResponseMessage.Content.ReadAsAsync<Result<TResponse>> 在 TResponse 值中始终为 null
【发布时间】:2018-05-07 15:10:18
【问题描述】:

当我使用 Postman 发出请求时,我得到:

这是我所期待的:

public class Result<TResultType>
{
    public int? ErrorId { get; protected set; } // Todo: Use it!!!
    public bool Success { get; protected set; }
    public ResponseErrorType ErrorType { get; protected set; }
    public string Message { get; protected set; }
    public TResultType Data { get; protected set; }
}

在这种情况下,TResultType 是:

public class SettingsResponse : BaseResponse
{
    public string LocationCode { get; set; }
    public string Ip { get; set; }
    public int Port { get; set; }
    public string ApplicationPrinterName { get; set; }
    public string MerchantNumber { get; set; }
    public string MessageControl { get; set; }
    public string PRIN { get; set; }
    public string SoftwareName { get; set; }
    public string SoftwareVersion { get; set; }
    public string SystemNumber { get; set; }
}

这就是我执行请求的方式:

using (HttpClient client = GetHttpClient())
{
    var responseTask = client.GetAsync($"{url}/{paramsStr}");
    if (await Task.WhenAny(responseTask, Task.Delay(TimeoutSeconds * 1000)) == responseTask)
    {
        var response = await responseTask;
        return response.IsSuccessStatusCode
            ? await response.Content.ReadAsAsync<Result<TResponse>>()
            : default(Result<TResponse>);
    }

    return Result<TResponse>.FromTimeout();
}

我总是在属性数据中得到空值,即使值存在于 json 响应中。

【问题讨论】:

  • 没有代码,我可以简单地提供“没有勺子”。
  • 那又怎样?您所展示的只是一个部分 JSON,其中的值被屏蔽了。你的问题是什么?您的电话是什么样的,期望它返回什么等?
  • a) 不要发布代码图片。 b) 如果没有minimal reproducible example,此问题将被关闭且无人回答。
  • 我们不想要您的对象,而是您如何执行请求以接收您的 HttpResponse
  • Images of code。请将您的代码复制并粘贴到您的问题中。并向我们​​展示您是如何执行您的请求的!!!!!!

标签: c# .net json.net dotnet-httpclient httpresponsemessage


【解决方案1】:

属性中带有公共设置器的结果类

public class Result<TResultType>
{
    public int? ErrorId { get; set; } // Todo: Use it!!!
    public bool Success { get; set; }
    public ResponseErrorType ErrorType { get; set; }
    public string Message { get; set; }
    public TResultType Data { get; set; }
}

它现在正在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多