Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Unable to call WEBAPI Service by using HttpClient.

$
0
0

Hi Am creating sample WebApi service. When i am trying to consume that service by using HttpClient ,am getting "Not Found" Error.

Its working on GetAsync method. but not working on PostAsync,PutAsync,DeletAsync.

My service code is

 Sample objsample = new Sample();
        // GET api/values

        public HttpResponseMessage Get()
        {
            var data=objsample.GetString();
            HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
            return obj;
           
        }

        // POST api/values
        [HttpPost]
        public HttpResponseMessage Post(string value)
        {
            var data = objsample.PostString(value);
            HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
            return obj;
        }

        // PUT api/values/5
        [HttpPut]
        public HttpResponseMessage Put(string value)
        {
            var data = objsample.PutString(value);
            HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
            return obj;
        }

        // DELETE api/values/5
        [HttpDelete]
        public HttpResponseMessage Delete(string id)
        {
            var data = objsample.PutString(id);
            HttpResponseMessage obj = Request.CreateResponse(HttpStatusCode.OK, data);
            return obj;
        }

My Client Code

 HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://localhost:45355/api/");

            client.DefaultRequestHeaders.Accept.Add(
                new MediaTypeWithQualityHeaderValue("application/json"));
           // HttpResponseMessage response = client.GetAsync("Values").Result;

            StringContent content = new System.Net.Http.StringContent("abc", Encoding.UTF8, "application/json");
// "abc"--String Value(input) //HttpResponseMessage response = client.PostAsync("values", content).Result; HttpResponseMessage response = client.DeleteAsync("values").Result; if (response.IsSuccessStatusCode) { var s = response.Content.ReadAsStringAsync(); string _resukt = s.Result; }

Could you please help me on that.How to call /pass string value to service by using PutAsync,PostAsync,DeleteAsync.

Thanks,

Sundar.


Viewing all articles
Browse latest Browse all 4850


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>