Using MVC, returning adhoc Json was easy.
return Json(new { Message = "Hello"});
I am looking for this functionality using the new web API.
public HttpResponseMessage<object> Test() { return new HttpResponseMessage<object>(new { Message = "Hello" }, HttpStatusCode.OK); }
This throws an exception because the DataContractJsonSerializer cannot handle anonymous types.
I replaced this with JsonNetFormatter based on Json.Net. It works if I use
public object Test() { return new { Message = "Hello" }; }
but I see no reason to use the web API, if I do not return HttpResponseMessage , I would be better off sticking to vanilla MVC. If I try to use:
public HttpResponseMessage<object> Test() { return new HttpResponseMessage<object>(new { Message = "Hello" }, HttpStatusCode.OK); }
It serializes all HttpResponseMessage .
Can someone lead me to a solution in which I can return anonymous types inside an HttpResponseMessage ?
Magpie Apr 12 '12 at 12:15 2012-04-12 12:15
source share