1

I have a web api which I'm calling (this is working correctly)

I call this like so

public ActionResult Index()
    {
        var mod = Checksomething();
        return View();
    }

    public async Task Checksomething()
    {
        try
        {
            var client = new HttpClient();
            var content = new StringContent(JsonConvert.SerializeObject(new UserLogin { EmailAddress = "[email protected]", Password = "bahblah" }));
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = await client.PostAsync("http://localhost:28247/api/UserLoginApi2/CheckCredentials", content);

            var value = await response.Content.ReadAsStringAsync();

            // I need to return UserProfile

            var data = JsonConvert.DeserializeObject<UserProfile[]>(value);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }

My web api passes back a model called UserProfile, I'm having great difficulty trying to return data back to the Index controller, would someone please enlighten me.

1 Answer 1

3

You need to change your method signature to use the generic version of Task

public async Task<ActionResult> Index()
{
    UserProfile[] profiles = await Checksomething();

    if (profiles.Any())
    {
          var user = profiles.First();
          string username = user.FirstName;

          // do something w/ username
    }
    return View();
}

public async Task<UserProfile[]> Checksomething()
{
    try
    {
        var client = new HttpClient();
        var content = new StringContent(JsonConvert.SerializeObject(new UserLogin { EmailAddress = "[email protected]", Password = "bahblah" }));
        content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        var response = await client.PostAsync("http://localhost:28247/api/UserLoginApi2/CheckCredentials", content);

        var value = await response.Content.ReadAsStringAsync();

        // I need to return UserProfile

        return JsonConvert.DeserializeObject<UserProfile[]>(value);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

The returned Task will be unwrapped and your caller will be given the Result of the Task, which in this case will be UserProfile[]

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

8 Comments

in my index I get an error under UserProfile[] it says can't convert system source type Threading.thread.Tasks.task<ProjectWebApi.Models.UserProfile> to target type ProjectWebApi.Models.UserProfile
That doesn't work either I now get the following error on the index action result the async method lacks 'await' operators and will run synchronously
Your action is public async Task<ActionResult> Index()?
didn't notice you made a modification that changed fixed that error, but now i get an error on checksomething it says member with the same signature is already declared
If an array is coming back, and only 1 user is in it, you can do var user = profiles.FirstOrDefault().FirstName to access the first item in the array. Make sure and check for null though. If the collection is empty, user will be null.
|

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.