I am fairly new to Web API so please forgive a stupid question
I have a Web API 2 method......
[System.Web.Http.HttpPost]
public MyAPIController AddItemToBasket(Guid b, Guid l, Guid a,
Guid cid, int d, int p, int q)
{
..blah blah
}
I am putting together a Winform test app.
I cannot seem to pass the parameters into the method. I see many examples where a POST method has an object as a parameter and that seems straightforward, but bizarly, passing in simple types seems far more of a head ache.
It seems I need to populate a HTTPContent var and pass that but I cannot see how to do that.
Or, should I just wrap these parameters in an object. Either way - I'd like to know how to do this for future use.
TIA,
Ant
By default for POST
actions, the Web Api framework will seek the parameters from the message body of the request. If you are trying to pass the params from the querystring you need to use the [FromUri]
attribute.
[HttpPost]
public IHttpActionResult AddItemToBasket([FromUri]Guid b, [FromUri]Guid l, [FromUri]Guid a,
[FromUri]Guid cid, [FromUri]int d, [FromUri]int p,
[FromUri]int q)
{
..blah blah
}
or even better make a NewItem
Object
and mark that with the attribute only once.
public class NewItem
{
public Guid b { get; set; }
public Guid l { get; set; }
public Guid a { get; set; }
public Guid cid { get; set; }
public int d { get; set; }
public int p { get; set; }
public int q { get; set; }
}
[HttpPost]
public IHttpActionResult AddItemToBasket([FromUri]NewItem item)
{
..blah blah
}
However I strongly suggest you go with the flow and use POST
api actions the way they are intended. That is post the new item data to the message body.