Search code examples
c#asynchronousgetactionresult

How to Access the Result of GET Method From a Class?


I have a get method. This method calls an endpoint from a different project and gets customer information. I want to use this customer's information in another class. But I don't know how to access it.

It is an option to write the information I get with the method into the database and then read it from there, but the number of customers may be high and there may have been an update in the customer information. That's why I have to call whenever I need it.

My GetCustomerInformation(customerIdList) method returns that : return Ok(customerInfoList);

And when I want to get customerInfoList in another class, I tried something like that:

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

It returns an ActionResult, how can I get the customerInfoList as List?


Solution

  • Assuming that

    var customerList =_customerInfoController.GetCustomerInformation(customerIdList)
    

    is an external Web API call.. the type of customerList will be an HttpResponseMessage. So we need to covert that data into a string and then to a specific object and then you can access the list.

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

    You may need to create a class with all the specified properties of customer list as YourSpecificObject..