locked
How to pass several objects to web api method? RRS feed

  • Question

  • Hi,

    I've got controller that inherits from ApiController. It has following method:

    [HttpPost]
    
    public Product Add(Product prod)
    {
     try
     {
       if (prod == null)
       {
          throw new ArgumentNullException("Product");
       }
       db.Products.AddObject(prod);
       db.SaveChanges();
    
       int id = prod.ID;
       Product p = db.Products.Where(x => x.ID == id).First();
       return p;
    
     }
     catch (Exception e)
     {
       return null;
     }
    }

    To invoke this method I use client method shown below:

    public Product Add(Product prod) {

    ApiServiceUrl = "http://localhost:55933/Api/Product"; using (WebClient client = new WebClient()) { client.Headers[HttpRequestHeader.ContentType] = "application/json"; var jsonObj = JsonConvert.SerializeObject(prod); var dataString = client.UploadString(ApiServiceUrl, jsonObj); var data = JsonConvert.DeserializeObject<Product>(dataString); return data; } }

    and it works fine.

    I'm wondering if it's possible to invoke Web Api method that has several parameters(eg. Product prod, SomeObject info, String desc). How should look client method to achieve that?

    Regards,

    borowa

    Tuesday, April 2, 2013 10:17 PM

Answers

  • Small tweak using your code:

    public Product Add(List<Product> prodlist)
    {
      ApiServiceUrl = "http://localhost:55933/Api/Product";
      using (WebClient client = new WebClient())
        {
           client.Headers[HttpRequestHeader.ContentType] = "application/json";
           var jsonObj = JsonConvert.SerializeObject(new {prods = prodlist.ToArray()});
           var dataString = client.UploadString(ApiServiceUrl, jsonObj);
           var data = JsonConvert.DeserializeObject<Product>(dataString);
           return data;
        }
    }

    Now modify you web method to:

    public Product Add(List<Product> prods)
    {
       //You should be able to change accordingly
    }

    • Edited by cheong00Editor Wednesday, April 3, 2013 2:56 AM
    • Proposed as answer by Carsten Siemens Saturday, April 6, 2013 10:03 PM
    • Marked as answer by Bob Shen Monday, April 15, 2013 9:10 AM
    Wednesday, April 3, 2013 2:50 AM
    Answerer

All replies

  • For asp.net question, please send here:

    http://forums.asp.net


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Wednesday, April 3, 2013 2:14 AM
  • <dir>

    Front end code

    Just put the parameter in your backend.

    $.ajax({
        url: '/Inventory/TransferParts',
        data: $.param({ id: id, name:name }),
        type: 'POST',
        success: function () {
         self.model.fetch();
         $.growlUI('System Notification', 'Your changes have been saved!');
        },
        error: function (xhr) {
         showError(xhr.responseText);
        }
       });</dir>

    chanmm

    Wednesday, April 3, 2013 2:23 AM
  • Small tweak using your code:

    public Product Add(List<Product> prodlist)
    {
      ApiServiceUrl = "http://localhost:55933/Api/Product";
      using (WebClient client = new WebClient())
        {
           client.Headers[HttpRequestHeader.ContentType] = "application/json";
           var jsonObj = JsonConvert.SerializeObject(new {prods = prodlist.ToArray()});
           var dataString = client.UploadString(ApiServiceUrl, jsonObj);
           var data = JsonConvert.DeserializeObject<Product>(dataString);
           return data;
        }
    }

    Now modify you web method to:

    public Product Add(List<Product> prods)
    {
       //You should be able to change accordingly
    }

    • Edited by cheong00Editor Wednesday, April 3, 2013 2:56 AM
    • Proposed as answer by Carsten Siemens Saturday, April 6, 2013 10:03 PM
    • Marked as answer by Bob Shen Monday, April 15, 2013 9:10 AM
    Wednesday, April 3, 2013 2:50 AM
    Answerer