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

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

ReadAsAsync<T>() 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.

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.