0

How to bind the json/xml response of WebApi to a model type? Like if I have a model User and my api returns list of users in json/xml format then how can I automatically bind the response to List<users>? In WCF client with WebHttpBinding once we create channel then we get reference to the service interface and can call methods like RPC and use models.

With WebApi we have the ability to process the response asyn way which is good. But I am not able to get how we can automatically bind or cast the response to a model like User or List<User>.

Abhijit-K
  • 3,569
  • 1
  • 23
  • 31

1 Answers1

3

if your rest client is System.Net.Http.HttpClient :

        var result = new List<User>();
        var client = new HttpClient();
        client.GetAsync("http://sample.net/api/user/GetList").ContinueWith((task) =>
        {
            HttpResponseMessage response = task.Result;

                response.Content.ReadAsAsync<List<User>>().ContinueWith((readTask) =>
                {
                    result = readTask.Result;
                });
        }).Wait();
mchouteau
  • 56
  • 6
  • ReadAsAsync() is an extension method. You shall be needing a reference to System.Net.Http.Formatting. For some odd reason, this didn't appear in my listing of system references. I needed to search for "formatting" to get it to appear. – bbsimonbb Jun 14 '16 at 09:45