locked
How to call my web api action by http cient RRS feed

  • Question

  • User264732274 posted

    my web api action return data bit different way. see my action code

    [HttpGet]
    [Route("GetByID/{customerID}")]
    public HttpResponseMessage GetCustomer(string customerID)
    {
        Customer customer = repository.Get(customerID);
        if (customer == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        Response response = new Response(1, "SUCCESS", customer);
        return Request.CreateResponse(HttpStatusCode.OK, response);
    
    }
    

    my Response class code in which i wrapped the data as object and return to client.

       public class Response
        {
            int ResponseCode;
            string ResponseMessage;
            object ResponseData;
            public Response(int code, string message, object data)
            {
                ResponseCode = code;
                ResponseMessage = message;
                ResponseData = data;
            }
        }
    

    now i like to know how could i call my web api by http client and extract customer data and show the customer data through datagridview.

    Thursday, August 25, 2016 3:10 PM

Answers

  • User36583972 posted

    Hi sudip_inn,

    Try the following code.

                 using (var client = new System.Net.Http.HttpClient())
                 {
                   
                    client.BaseAddress = new Uri("baseUrl");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var response =  client.GetAsync("/api/Customer/GetAll").Result;
                    string res = "";
                    using (HttpContent content = response.Content)
                    {
                        // ... Read the string.
                        Task<string> result =  content.ReadAsStringAsync();
                        res = result.Result;
                    }
                    Response obj = JsonConvert.DeserializeObject<Response>(res);
                    List<Customer> listcoustom = new List<Customer>();
                    listcoustom.Add((Customer)JsonConvert.DeserializeObject <Customer> (obj.ResponseData.ToString()));
                    GridView1.DataSource = listcoustom;
                    GridView1.DataBind();
                }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 9:04 AM

All replies

  • User36583972 posted

    Hi sudip_inn,

    You can not get the complex type from Get(GetCustomer ) method (you can test it by yourself). You need to change your design ideas.

    You can refer the following links.

    HTTP Operations GET, POST, PUT and DELETE From .NET Client:

    http://www.c-sharpcorner.com/UploadFile/dacca2/http-request-methods-get-post-put-and-delete/

    Calling a Web API From a .NET Client in ASP.NET Web API 2:

    http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

    Best Regards,

    Yohann Lu

    Friday, August 26, 2016 5:38 AM
  • User36583972 posted

    Hi sudip_inn,

    Try the following code.

                 using (var client = new System.Net.Http.HttpClient())
                 {
                   
                    client.BaseAddress = new Uri("baseUrl");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var response =  client.GetAsync("/api/Customer/GetAll").Result;
                    string res = "";
                    using (HttpContent content = response.Content)
                    {
                        // ... Read the string.
                        Task<string> result =  content.ReadAsStringAsync();
                        res = result.Result;
                    }
                    Response obj = JsonConvert.DeserializeObject<Response>(res);
                    List<Customer> listcoustom = new List<Customer>();
                    listcoustom.Add((Customer)JsonConvert.DeserializeObject <Customer> (obj.ResponseData.ToString()));
                    GridView1.DataSource = listcoustom;
                    GridView1.DataBind();
                }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 28, 2016 9:04 AM