This project is read-only.

Problems binding content parameter

Topics: Web Api
Jul 7, 2011 at 10:08 PM

I could be wrong but I think there is a restriction to only one content parameter per operation? That is only one method parameter is allowed to be mapped from the http request.

I had hoped to write an method with two input parameters and use separate operation handlers to read out the values, one from the content and one from a http header.

I can understand the idea that the http content should only map to one parameter but feel that headers should be allowed to be treated separately in the same that query parameters are.

Jul 7, 2011 at 10:25 PM

It is possible to add an operation handler that injects new parameters into the operations, namely using information from the headers.

Jul 7, 2011 at 11:37 PM

pmhsfelix you are right I got confused and got this the wrong way round. My problem is not too many content parameters but too few.

I have a method with two parameters one that has a operation handler with excepts the HttpRequest and converts a header into the parameter value. The other parameter is a HttpRequestMessage object. How am I suppose to identify the HttpRequest parameter passed into the method as a content parameter so I do not get the error.

The HttpOperationHandlerFactory is unable to determine the input parameter that should be associated with the request message content for service operation 'PUT'. If the operation does not expect content in the request message use the HTTP GET method with the operation. Otherwise, ensure that one input parameter either has it's IsContentParameter property set to 'True' or is a type that is assignable to one of the following: HttpContent, ObjectContent`1, HttpRequestMessage or HttpRequestMessage`1.

Jul 7, 2011 at 11:52 PM

Well, if one of your parameters is a HttpRequestMessage, then that exception should not occur, i.e., there shouldn't be any doubt about the parameter associated with the request message.

However, I believe that there is a internal error on that resolution logic. Try using HttpRequestMessage<T> instead of the non-generic HttpRequestMessage.

Jul 7, 2011 at 11:59 PM

Yeah thanks pmhsfelix I just tried it with HttpRequestMessage<T> and it works.

Jul 8, 2011 at 2:08 AM

@gambrose we are planning to fix this. For now another option is you can use ObjectContent<T> over HttpRequestMessage<T> if you do not need the request headers. This will save you a bit of typing as you you can just call ReadAs() directly off of the content rather than Content.ReadAS().

Thanks for your patience, and Pedro thanks for helping :-)

Sep 26, 2011 at 10:56 PM

@gblock this still appears to be an issue in preview 5 unless I'm mistaken. Is there a new way of specifying the content parameter or is this still a work in progress?

Also the method name 'ReadAs()' without any type parameters or arguments is very odd. Should this just be 'Read()' in this case?

Sep 27, 2011 at 8:21 AM

@dcstraw i've pinged the team on this.

As to ReadAs, it is symmetrical with ReadAs<T>. When the content is already strongly typed i.e. if you have HttpRequestMessage<T> / HttpResponseMessage<T> we already know T. I hear you on the oddness though.