Where is the source for HttpResponseMessage<T>

Topics: ASP.NET MVC, ASP.NET Web API, General
Mar 29, 2012 at 2:44 PM

I downloaded the source code and built the solution successfully. However, when I replaced the DLLs I got from NuGet with the ones I compiled, the System.Net.Http.Formatting assembly no longer contains the definition for HttpResponseMessage<T>

I'm sure I'm missing something...any help would be great.

Thanks,

Rafael

Mar 29, 2012 at 4:35 PM
rjuliano wrote:

the System.Net.Http.Formatting assembly no longer contains the definition for HttpResponseMessage<T>

You are not missing anything. We have decided to remove HttpResponseMessage<T> as part of a larger redesign of how content negotiation works.

Mar 29, 2012 at 4:44 PM

We are working on a blog explaining the changes in HttpResponseMessage<T>, HttpRequestMessage<T>, and ObjectContent<T> in more details as we have simplified a bunch and made things more consistent.

The reason for the changes was that we ran into some issues with the HttpResponseMessage<T> and HttpRequestMessage<T> model. First, it was not possible to guarantee that the <T> carried in the HttpContent in a request or response actually was a <T>. As requests and responses are completely mutable they can contain any HttpContent implementation without any regard to HttpResponseMessage<T> or HttpRequestMessage<T>.

Second, HttpResponseMessage<T> and HttpRequestMessage<T> behaved slightly different depending on whether they were used on client side or server side.

Third, we wanted to make the common case simpler so that you always see HttpRequestMessage and HttpResponseMessage instances and not have to worry about whether they were generic or not. Now they are always the same.

Here’s a rough mapping from the old to the new model:

On the client side, all you now do when wanting to PUT or POST JSON (or XML) is to use these methods exposed directly on HttpClient:

HttpClient.PostAsJsonAsync<T>(T value) sends “application/json”

HttpClient.PostAsXmlAsync<T>(T value) sends “application/xml”

On the server side, in a controller you

ApiController.Request.CreateResponse<T> (T) sends content in the best format determined by content negotiation

You can also say: I want to send it in JSON explicitly in which case we will do that:

ApiController.Request.CreateResponse<T> (T, “application/json”) sends content in “application/json”

You can also cook up an ObjectContent yourself where you are in complete control of the formatter you want to use

HttpResponseMessage response = new HttpResponseMessage();

response.Content = new ObjectContent<T>(T, myFormatter, “application/some-format”);

Hope this helps – as I said we will be sending out more details on this.

Henrik

From: marcind [email removed]
Sent: Thursday, March 29, 2012 9:36 AM
To: Henrik Frystyk Nielsen
Subject: Re: Where is the source for HttpResponseMessage<T> [ASPNETWebStack:350492]

From: marcind

rjuliano wrote:

the System.Net.Http.Formatting assembly no longer contains the definition for HttpResponseMessage<T>

You are not missing anything. We have decided to remove HttpResponseMessage<T> as part of a larger redesign of how content negotiation works.

Mar 29, 2012 at 4:52 PM

Got it...that makes sense. That was going to be my follow up question, but you covered it.

Thanks a bunch.

Jun 6, 2012 at 8:36 AM

Hi HenrikN have you guys managed to get the blog post completed yet? I am really stuck on this and have gone around in circles for the last 1/2 day trying to find a solution.

On my client mvc4 app I try 

 using (var client = new HttpClient()) 

However I dont see any client.PostAsJsonAsync. Do you have a small example of what is should look like on the client and the server?

Jun 6, 2012 at 9:42 AM
Make sure you have a "using System.Net.Http;" in your source file, and that you have linked against System.Net.Http.Formatting.dll.
Jun 6, 2012 at 3:25 PM

It's still on the list -- we are busy finalizing the bits. If you upgrade to the RC bits then you will see PostAsJsonAsync and friends that should make the samples above work.

Thanks,

Henrik

Jun 18, 2013 at 4:59 AM
What about a HttpClient.PostAsForm<T>( T value ) for the application/x-www-form-urlencoded content type?