0

I tried the solutions posted on StackOverflow, but none of them worked. I have a method calling a web service. The code is as following and I keep getting a compiler error:

    public async Task<ActionResult <List<items>>>getitems()
    {
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("https://localhost:5001/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));
        var Res = await client.PostAsJsonAsync("api/sub", ids);
        Res.EnsureSuccessStatusCode();
        if (Res.IsSuccessStatusCode)
        {
            var Response = Res.Content.ReadAsStringAsync().Result;   
            var sub = JsonConvert.DeserializeObject<JArray>(Response);
            List<items> item = sub.ToObject<List<items>>();
            return Ok(item);
        }
    }

Then I call the method from a different class as following:

    public async Task<List<items>> getService(List<string> ids)
    {
        var IdentificationIdsToOrder = new JObject();
        foreach (var id in ids)
        {
            var newId = new JProperty("ids", id);
            IdentificationIdsToOrder.Add(newId);
        }
        _controller = new getitems();
        var Res = await _controller.getitems();         
        var ItemList = Res.Result;
        return ItemList;
    }
}

Here it goes wrong with the return value and I can't compile.

What I'm missing?

10
  • There are inconsistencies with the code shown. Provide a minimal reproducible example that clarifies your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. Commented Mar 10, 2019 at 0:21
  • @Nkosi sorry, I was trying different apporches and mixed some code together, following different examples from Stack overflow Commented Mar 10, 2019 at 0:37
  • This is understood. But in order to get help you need to have a least a base set of code that we can use to reproduce your problem and find a solution. Commented Mar 10, 2019 at 0:38
  • Yes, I edited the post method. which is actually working when I tested with postman. but the issue is that I can't get to work when I call it from a different class Commented Mar 10, 2019 at 0:41
  • 1
    ActionResult is used with Mvc to return response when the action method is called. I would extract your api calling code into another class that simply returns the List<> and then use that in the getitems action and in your getService method. Commented Mar 10, 2019 at 0:58

1 Answer 1

1

In getService method you should use var ItemList = Res.Value; in place of var ItemList = Res.Result;

public async Task<List<items>> getService(List<string> ids)
{
    var IdentificationIdsToOrder = new JObject();
    foreach (var id in ids)
    {
        var newId = new JProperty("ids", id);
        IdentificationIdsToOrder.Add(newId);
    }

    _controller = new getitems();
    var Res = await _controller.getitems();         
    var ItemList = Res.Value;
    return ItemList;

}
Sign up to request clarification or add additional context in comments.

1 Comment

this worked only after I removed the action result from the method I'm calling. That is why I up-voted Eugene S. comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.