I asked this question couple of days ago in a different thread and didn't get it answered, so will open a dedicated thread for this specific topic. Hopefully, we'll resolve it here.
I have Get and Post actions and an {id} url segment within the single route:
"{controller}/{id}"
On Get the id argument of the action is set correctly. However on Post, it is null. The second 'content' argument is set correctly. Both actions are in the same controller:
RoutingController : ApiController
{
public HttpResponseMessage Get(string id)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
public HttpResponseMessage Post(string id, Content content)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
Calling code for Get
var response = client.GetAsync("routing/someid").Result;
The calling code for Post:
var content = new HttpResponseMessage<Content>(new Content { Value = "some value"}).Content;
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml");
response = client.PostAsync("routing/someid", content).Result;
and the Content class:
class Content { public string Value { get; set; } }
The POST verb is semantically used to create a new element in your collection.
Have you tried using the more semantically correct PUT verb to indicate that you are actually editing an element?
It actually makes sense that POST would not work as you are not supposed to have an ID prior to adding the element.
you're right. However it is not a good explanation why it can possibly be not working. In fact I didn't think about semantics at all when chose POST method - just was choosing the first one which "accepts arguments" in the body.
Vistor
Member
12 Points
9 Posts
URL segment binding works on GET, not on POST
Mar 07, 2012 05:57 PM|LINK
I asked this question couple of days ago in a different thread and didn't get it answered, so will open a dedicated thread for this specific topic. Hopefully, we'll resolve it here.
I have Get and Post actions and an {id} url segment within the single route:
"{controller}/{id}"On Get the id argument of the action is set correctly. However on Post, it is null. The second 'content' argument is set correctly. Both actions are in the same controller:
RoutingController : ApiController { public HttpResponseMessage Get(string id) { return new HttpResponseMessage(HttpStatusCode.OK); } public HttpResponseMessage Post(string id, Content content) { return new HttpResponseMessage(HttpStatusCode.OK); } }Calling code for Get
var response = client.GetAsync("routing/someid").Result;The calling code for Post:
var content = new HttpResponseMessage<Content>(new Content { Value = "some value"}).Content; content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/xml"); response = client.PostAsync("routing/someid", content).Result;and the Content class:
class Content { public string Value { get; set; } }What is that I'm missing?
Thanks,
Vitaly
Arch4ngel
Member
175 Points
31 Posts
ASPInsiders
MVP
Re: URL segment binding works on GET, not on POST
Mar 07, 2012 06:13 PM|LINK
The POST verb is semantically used to create a new element in your collection.
Have you tried using the more semantically correct PUT verb to indicate that you are actually editing an element?
It actually makes sense that POST would not work as you are not supposed to have an ID prior to adding the element.
raghuramn
Member
248 Points
64 Posts
Microsoft
Re: URL segment binding works on GET, not on POST
Mar 07, 2012 06:40 PM|LINK
I tried it out and it is working fine for me. Can you send me a repro (a solution) (ranadimi at microsoft) ?
Vistor
Member
12 Points
9 Posts
Re: URL segment binding works on GET, not on POST
Mar 07, 2012 09:16 PM|LINK
sent. Thanks for your help.
Vistor
Member
12 Points
9 Posts
Re: URL segment binding works on GET, not on POST
Mar 07, 2012 09:19 PM|LINK
you're right. However it is not a good explanation why it can possibly be not working. In fact I didn't think about semantics at all when chose POST method - just was choosing the first one which "accepts arguments" in the body.