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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…