2

I am using xamarin c# and Mvvmcross.

I am able to do a Get using the below code which returns a List of MyObject.

How can I change this so that I can edit a MyObject?

I would like to do soothing like:

url = "http://address/api/MyObject/myId";
request.Method = "Put";
request.SecondParamOfWebAPICall = new MyObject(){ObjectId = "myId", FieldToChange = "123"};

But then i am not sure how to do this?. This is the web API method I want to call

    // PUT api/MyObject/id
    public IHttpActionResult PutMyObject(int id, MyObject myObject)
    {
         //Use param id to get the required object to edit 
    }

This is my code which does a Get:

    public void GetMyObjectItems(Action<MyObject> success, Action<Exception> error)
    {
        var url = "http://address/api/MyObject";

        var request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            request.BeginGetResponse(result => ProcessResponse(success, error, request, result), null);
        }
        catch (Exception exception)
        {
            error(exception);
        }
    }

    private void ProcessResponse(Action<MyObject> success, Action<Exception> error, HttpWebRequest request, IAsyncResult result)
    {
        try
        {
            var response = request.EndGetResponse(result);
            //var locationList = response.Content.ReadAsAsync<MyObject>().Result;

            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var text = reader.ReadToEnd();
                var list = _jsonConverter.DeserializeObject<MyObject>(text);

                success(list);
            }
        }
        catch (Exception exception)
        {
            error(exception);
        }
    }
4
  • so your class has property ObjectId but in json it has name myId and for deserialization you want to set some mapping, am I right? Commented Apr 16, 2014 at 7:51
  • My Choper. I have update the question so it's more clear. I have said the webAPI method I am trying to call and how I am attempting it. Let me know if it's not clear. Thanks Commented Apr 16, 2014 at 11:40
  • Try searching for questions about HttpWebRequest and PUT - e.g. stackoverflow.com/questions/13642401/… and stackoverflow.com/questions/9054378/… - there should be plenty out there on this already. Alternatively, install HttpClient from nuget and then use that - switching to async will rock your world! Commented Apr 16, 2014 at 13:06
  • I'd have thought you would need to use the request.write() method, to 'PUT' information and then process the response. Commented Apr 16, 2014 at 13:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.