【问题标题】:How to Access the Result of GET Method From a Class?如何从类中访问 GET 方法的结果?
【发布时间】:2021-07-06 08:07:56
【问题描述】:

我有一个 get 方法。此方法调用来自不同项目的端点并获取客户信息。我想在另一个班级使用这个客户的信息。但我不知道如何访问它。

可以选择将我通过该方法获得的信息写入数据库,然后从那里读取,但客户数量可能很高,并且客户信息可能已经更新。这就是为什么我必须随时打电话。

我的GetCustomerInformation(customerIdList) 方法返回:return Ok(customerInfoList);

当我想在另一个班级获得customerInfoList 时,我尝试了类似的方法:

var customerList = _customerInfoController.GetCustomerInformation(customerIdList).Result;

它返回一个ActionResult,我怎样才能得到customerInfoListList

【问题讨论】:

    标签: c# asynchronous get actionresult


    【解决方案1】:

    假设

    var customerList =_customerInfoController.GetCustomerInformation(customerIdList)
    

    是一个外部 Web API 调用.. customerList 的类型将是 HttpResponseMessage。所以我们需要将这些数据转换成一个字符串,然后转换成一个特定的对象,然后你就可以访问这个列表了。

    var response = customerList .Content.ReadAsStringAsync().Result;
    var data= JsonConvert.DeserializeObject<List<YourSpecificObject>>(response);
    

    您可能需要创建一个类,其中客户列表的所有指定属性为YourSpecificObject..

    【讨论】:

      【解决方案2】:

      首先在你要使用的项目中添加API所在的项目引用(如下图)

      然后你的新类(在其他项目中)可以这样实现。

      @using APIProject;
      public IList<YourSpecificObject> customerList { get; set; }
      public async Task<IActionResult> OnPost(customerIdList)
      {
        customerList =_customerInfoController.GetCustomerInformation(customerIdList)
      }
      

      希望这会有所帮助。谢谢。

      【讨论】:

        猜你喜欢
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 2017-04-19
        • 2021-07-09
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多