Click here to Skip to main content
15,847,263 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a exercise about ASP.NET Web API - Implementing REST Services with Web API.

I stuck with that : "b. Add a private method named CallPersonListServiceAsync() receiving an HttpClient instance and a string for service url as parameters and returning IEnumerable.
i. Invoke the GetAsync() method on the client instance by using the service url parameter as argument. Get the Result property once the call finishes, and access the Content property which is the HttpContent instance kept inside the IHttpActionResult wrapper. Invoke the ReadAsAsync() generic method by supplying IEnumerable as the generic type and return the retrieved Result property from the method."

I searched for HttpContent and IHttpActionResult, but i didn't understand how should I use them.

Here is my code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebAPIDemo.Models;

namespace WebAPIDemo.Controllers
{
    public class PersonController : Controller
    {
        // GET: Person
        public ActionResult Index()
        {
            using (var client=new HttpClient())
            {
                var personListServiceUrl = GetPersonListServiceUrl();
                var personList = CallPersonListServiceAsync(client, personListServiceUrl);

                return View(personList);
            }
        }

        public string GetPersonListServiceUrl()
        {
             return Url.RouteUrl("DefaultApi", new { httproute = true, controller = "PersonService" }, Request.Url.Scheme);
        }

        private IEnumerable CallPersonListServiceAsync(HttpClient client, string serviceurl )
        {
            client.GetAsync(serviceurl);
            //todo

            
        }

    }
}

If you guys want, i can send whole lab and my code.
Thanks.
Posted

1 solution

I did it this way :
C#
private IEnumerable<person> CallPersonListServiceAsync(HttpClient client, string serviceurl )
{
    return client.GetAsync(serviceurl).Result.Content.ReadAsAsync<ienumerable><person>>().Result;


}</person></ienumerable></person>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900