32

In one of my Get request, I want to return an HttpResponseMessage with some content. Currently I have it working as follows:

var header = new MediaTypeHeaderValue("text/xml");
Request.CreateResponse(HttpStatusCode.OK, myObject, header);

However, since I am using the static Request, this becomes really difficult to test. From what I have read, I should be able to do the following:

return new HttpResponseMessage<T>(objectInstance);

However, seem to not be able to do this. Is it because I am using a older version of WebApi / .NET?


On a side note, I found that you could potentially create a response as follows:

var response = new HttpResponseMessage();
response.Content = new ObjectContent(typeof(T), objectInstance, mediaTypeFormatter);

What puzzled me is why do I have to add a mediaTypeFormatter here. I have added the media type formatter at the global.asax level.

Thanks!

Karan
  • 14,824
  • 24
  • 91
  • 157

1 Answers1

65

HttpResponseMessage<T> was removed after Beta. Right now, instead of a typed HttpResponseMessage we have a typed ObjectContent

If you manually create HttpResponseMessage using its default parameterless constructor, there is no request context available to perform content negotiation - that's why you need to specify the formatter, or perform content negotiation by hand.

I understand you don't want to do that - so use this instead:

HttpResponseMessage response = Request.CreateResponse<MyObject>(HttpStatusCode.OK, objInstance);

That would create the response message relying on the content negotiation performed against the request.

Finally, you can read more about content negotiation here On this link

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
Filip W
  • 27,097
  • 6
  • 95
  • 82
  • 4
    I miss the HttpResponseMessage, I think it was a better API, but Request.CreateResponse work fine. – Hugo Dec 31 '12 at 19:11
  • 3
    Remember that any controller method that uses `HttpResponseMessage` is very difficult to unit-test, so it's best to avoid using it if you can. – Roy Dictus May 27 '13 at 13:35
  • 7
    @RoyDictus I'm curious as to why it makes unit testing difficult, could you elaborate please? – Paul Manzotti Jul 04 '13 at 10:51
  • Because you can not cast `HttpContent` into the type of the object that values you might want to assert. – mono68 Sep 19 '13 at 16:25
  • Thanks @Filip...not exactly what I needed but it pointed me in the right direction. – MisterJames Apr 22 '14 at 16:10
  • @Roy-Dictus Not really sure why you'd say it's very difficult to unit test. Simply check to see if the response.Content is of type ObjectContent and its Value property is of the type you expect to get. A helper method can do this in 4 lines of code. If anything, it's annoying, but certainly not 'very difficult'... – Robba Aug 17 '15 at 10:16
  • thanks, its work me, Request.CreateResponse("pass iqueryable object") – adnan May 15 '16 at 04:43
  • I'm missing HttpResponseMessage as well as I need to read the method code to see what does the controller returns in the content :( – Ignacio Soler Garcia Feb 26 '18 at 15:49