1

This is related to and sort of a followup on this question

I need to know how I can pass and receive arguments to a Web API REST method.

I believe the way I am passing the args from the Client is correct, namely:

http://localhost:28642/api/InventoryItems/PostInventoryItem?ID=42?pksize=12?Description=ValuableDesc?vendor_id=venderado?dept=42?subdept=85?UnitCost=2.50?UnitList=3.75?OpenQty=25.25?UPC=12345?upc_pack_size=24?vendor_item=someVendorItem?crv_id=9898987

...where the first part:

http://localhost:28642/

...is the machine and port; the second part:

/api/InventoryItems/PostInventoryItem

...indicates the Controller and its method to run, and the final part (the rest) are the individual args to pass (in the format "?=").

Based on what is written here, namely:

"The method takes a parameter of type Product. In Web API, parameters with complex types are deserialized from the request body. Therefore, we expect the client to send a serialized representation of a product object, in either XML or JSON format."

...I was hoping that I could use this on my Controller:

   [Route("api/InventoryItems/PostInventoryItem")]   
   public HttpResponseMessage PostInventoryItem(InventoryItem ii)
    {
        _inventoryItemRepository.PostInventoryItem(ii.ID, ii.pksize, ii.Description, ii.vendor_id, ii.dept,
            ii.subdept, ii.UnitCost, ii.UnitList, ii.OpenQty, ii.UPC, ii.upc_pack_size, ii.vendor_item, ii.crv_id);
        // If/when this works, could do this instead:
        //_inventoryItemRepository.PostInventoryItem(ii);
        var response = Request.CreateResponse<InventoryItem>(HttpStatusCode.Created, ii);
        string uri = Url.Link("DefaultApi", new { id = ii.ID });
        response.Headers.Location = new Uri(uri);
        return response;
    }

...and that the client batch of args would get automatically bundled up into an InventoryItem object; but, alas, Web API is not able to do so (or I did something wrong/have as yet failed to do something right) and ii is null when the method is reached.

Do I have to do it different on the sending side, or on the receiving side, or both? What, exactly, do I need to amend or append?

UPDATE

If I try this instead in the Controller:

public HttpResponseMessage PostInventoryItem(string id, int pack_size, string description, string vendor_id, int department, int subdepartment, double unit_cost, double unit_list, double open_qty, string UPC_code, int UPC_pack_size, string vendor_item, long crv_id)
{
    _inventoryItemRepository.PostInventoryItem(id, pack_size, description, vendor_id, department, subdepartment,
        unit_cost, unit_list, open_qty, UPC_code, UPC_pack_size, vendor_item, crv_id);
    InventoryItem ii = new InventoryItem();
    ii.ID = id;
    ii.pksize = pack_size;
    . . .
    ii.crv_id = (int)crv_id;
    var response = Request.CreateResponse<InventoryItem>(HttpStatusCode.Created, ii);

...with a breakpoint on the "_inventoryItemRepository.PostInventoryItem()" line, it doesn't reach it, and the client, on attempting to send the URI to the server, pops up a message box that simply says, "null."

UPDATE 2

A combination of changing the Controller signature to this:

public HttpResponseMessage PostInventoryItem([FromUri] InventoryItem ii)

(hat's off to Mike Wasson for his article here)

...and (kudos to the wealthy beast of burden (richaux)) replacing all but the first "?" with "&":

http://localhost:28642/api/InventoryItems/PostInventoryItem?ID=42&pksize=12&Description=ValuableDesc&vendor_id=venderado&dept=42&subdept=85&UnitCost=2.50&UnitList=3.75&OpenQty=25.25&UPC=12345&upc_pack_size=24&vendor_item=someVendorItem&crv_id=9898987

...did the trick.

Community
  • 1
  • 1
B. Clay Shannon
  • 1,055
  • 124
  • 399
  • 759

1 Answers1

2

Based on the original question, you have two problems. First, as richaux explained, the format of the URL is incorrect. Second, you can have the contents of a complex type on the Uri, but it's, generally, bad practice. That's especially true when the Url you're setting up has "Post" in it's name. Users are going to expect that the entity passed to the controller is in the request body not the URL.

Scott Corbett
  • 380
  • 1
  • 7