title: Returning anonymous types with Web API
body: <p>When using MVC, returning adhoc Json was easy.</p>
  
  <pre><code>return Json(new { Message = "Hello"});
  </code></pre>
  
  <p>I'm looking for this functionality with the new Web API.</p>
  
  <pre><code>public HttpResponseMessage&lt;object&gt; Test()
  {    
     return new HttpResponseMessage&lt;object&gt;(new { Message = "Hello" }, HttpStatusCode.OK);
  }
  </code></pre>
  
  <p>This throws an exception as the <code>DataContractJsonSerializer</code> can't handle anonymous types.</p>
  
  <p>I have replaced this with this <a href="https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib.Formatting.JsonNet/JsonNetFormatter.cs" rel="noreferrer">JsonNetFormatter</a> based on <em>Json.Net</em>.
  This works if I use</p>
  
  <pre><code> public object Test()
   {
      return new { Message = "Hello" };
   }
  </code></pre>
  
  <p>but I don't see the point of using Web API if I'm not returning <code>HttpResponseMessage</code>, I would be better off sticking with vanilla MVC. If I try and use:</p>
  
  <pre><code>public HttpResponseMessage&lt;object&gt; Test()
  {
     return new HttpResponseMessage&lt;object&gt;(new { Message = "Hello" }, HttpStatusCode.OK);
  }
  </code></pre>
  
  <p>It serializes the whole <code>HttpResponseMessage</code>.  </p>
  
  <p>Can anyone guide me to a solution where I can return anonymous types within a <code>HttpResponseMessage</code>?</p>