Popular Posts

Sunday, March 2, 2014

How to self host the MVC Web API

Below is the complete solution for self hosting of Web API.

static HttpClient client = new HttpClient();
        static void Main(string[] args)
        {
            client.BaseAddress = new Uri("http://admin.xyz.com");
            ListAllProducts();
            ListProduct(1);
        }

 static void ListAllProducts()
        {
            HttpResponseMessage resp = client.GetAsync("api/product").Result;
            resp.EnsureSuccessStatusCode();
            var products = resp.Content.ReadAsStringAsync().Result;
        }

 static void ListProduct(int id)
        {
            var resp = client.GetAsync(string.Format("api/product/{0}", id)).Result;
            resp.EnsureSuccessStatusCode();
            var product = resp.Content.ReadAsStringAsync().Result;

        }

No comments:

Post a Comment