0

I have created a webApi that automatically created som methods for me. That Put-Method got created like this:

 // PUT: api/Actor/5
    [ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutActor(int id, Actor actor)
    {
        //Code that updates the actor
    }

But How can i call this method? Im thinking that it must start with:

HttpResponseMessage response =  client.PutAsJsonAsync("api/Actors/")  <-How can i add the two params?

According to some posts you cant do it without workarounds:

WebAPI Multiple Put/Post parameters

But it seems strange considering that the method got created to me automatically. Should be a standard way to do it no?

Community
  • 1
  • 1
bugsy
  • 111
  • 2
  • 13

1 Answers1

0

You could pass the id in the route, whereas the Actor in the payload using the following overload which takes as second argument an object:

HttpResponseMessage response = client.PutAsJsonAsync("api/Actors/123", new 
{
    actor = new Actor
    {
        Name = "John Smith",
        Age = 30,
    }
});
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you! I now understand how I can pass my actor. But I do not understand how I Can pass the Id in the route. Does this mean that I should make changes in the webapiconfig? – bugsy Jan 26 '15 at 18:27
  • Normally the default route setup includes the `id` token as part of the route. So all you have to do is pass it in there as shown in my answer: `"api/Actors/123"` where `123` is your id. – Darin Dimitrov Jan 26 '15 at 21:27