Question

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>.

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top