Is it possible to bind a model from both the Uri and Body?
For instance, given the following:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
public class ProductsController : ApiController
{
public HttpResponseMessage Put(UpdateProduct model)
{
}
}
public class UpdateProduct
{
int Id { get; set;}
string Name { get; set; }
}
Is it possible to create a custom binder so that a PUT
to
/api/products/1
with a JSON body of:
{
"Name": "Product Name"
}
will result in the UpdateProduct
model populated with Id = 1
and Name = "Product Name"
?
Update
I understand that I could change the action signature to
public HttpResponseMessage Put(int id, UpdateProduct model)
{
}
However as stated in the question, I specifically want to bind to a single model object
I have also posted this question to the WebApi Codeplex discussion forum
See Question&Answers more detail:os