Sunday, May 27, 2012

MVC Webapi

Today, I'm posting about WebApi. Recently, I've looked some example of Webapi on other blogs. Recently, found some of other people's conversation from other sites.

1.Introduction to HttpClient
HttpClient provides a flexible and extensible API for accessing all things exposed through HTTP. We used to use it for a while as part of WCF Web API. It is now part of ASP.NET Web API and in .NET 4.5 which is make developer to easier to develop.
System.Net.Http: it provides the basic HttpClient and related classes
System.Net.Http.Formatting: Adds support for serialization, deserialization as well as for many additional features building on top of System.Net.Http
System.Json: it is for JsonVaue which is a mechanism for reading and manipulating JSON documents
Please see below the sample code which I found it on msdn blog (http://code.msdn.microsoft.com/Introduction-to-HttpClient-4a2d9cee)

1: staticvoid Main(string[] args)

2: {

3: // Create an HttpClient instance

4: HttpClient client = new HttpClient();

5:

6: // Send a request asynchronously continue when complete

7: client.GetAsync(_address).ContinueWith(

8: (requestTask) =>

9: {

10: // Get HTTP response from completed task.

11: HttpResponseMessage response = requestTask.Result;

12:

13: // Check that response was successful or throw exception

14: response.EnsureSuccessStatusCode();

15:

16: // Read response asynchronously as JsonValue and write out top facts for each country

17: response.Content.ReadAsAsync<JsonArray>().ContinueWith(

18: (readTask) =>

19: {

20: Console.WriteLine("First 50 countries listed by The World Bank...");