none
Calling a Web API method in code - having trouble RRS feed

  • Question

  • I am using Web API to create a couple of methods. I am confused because I can call some of the methods just fine, but others return 404 - Not Found.

    For example, I can call

     [HttpPost]
            public SampleFields UpdateKeywordsForSampleNumber(SampleFields fields)
            {
                fields.Status = 999; //for testing just set to 999
                return fields;
            }

    using client code like this:

        
         using(WebClient client = new WebClient() )
                {
                        client.Headers[HttpRequestHeader.ContentType] = "application/json";

                        SampleFields fields = new SampleFields() {SampleNumber = 400012345,LastName = "Oakly",Officer = "Biden",Status = 1};

                        string json = JsonConvert.SerializeObject(fields);

                        string strUrl = _baseUri.AbsoluteUri + ProductsUrl + "/UpdateKeywordsForSampleNumber";

                        var result = client.UploadString(strUrl, json);

                        var apiResult = JsonConvert.DeserializeObject(result);

                        Console.WriteLine("Result from CallWithWebClient(): {0}", apiResult);   //method called and Status field changed to 999

                 }


    but I cannot figure out how to call a method that looks like this:


      [HttpPut]
            public int UpdateSample(string sampleNumber, long docId, short docType )
            {

                return 1;
            }


    If I call it like this:

           using(WebClient client = new WebClient() )
                {
                        client.Headers[HttpRequestHeader.ContentType] = "application/json";


                        string strUrl = strUrl = _baseUri.AbsoluteUri + ProductsUrl + "/UpdateSample";

                        string json = "{\"sampleNumber\":\"1\",\"docId\":2,\"docType\":3}"; //format as JSON?

                        var result = client.UploadString(strUrl,"PUT" ,json); //EXCEPTION 404 - METHOD NOT FOUND

       ...

    Thanksm


    Hedley

    Tuesday, May 28, 2013 8:36 PM

Answers