title: Can I manually hard code a JSON object to be returned by ASP.NET web API?
body: <p>I'm used to doing this in Django (similar to Ruby on Rails) where in some cases I need to hard code a JSON response object for the client to be able to interpret, but I've been searching everywhere online on figuring out how to do this with ASP.NET web API and I can't find anything on this, ASP.NET web API seems to be forcing me to create a class to represent a JSON response for every URI controller. </p>
  
  <p>For example, here's the only way I know for manually creating a JSON response:</p>
  
  <p>1.) I first need to create the class to represent the response object</p>
  
  <pre><code>public class XYZ_JSON
  {
      public string PropertyName { get; set; }
      public string PropertyValue { get; set; }
  }
  </code></pre>
  
  <p>2.) Then I need to properly write up the URI controller that'll return an "XYZ_JSON" that I've just defined above:</p>
  
  <pre><code>// GET: api/ReturnJSON
  public XYZ_JSON Get()
  {
      XYZ_JSON test = new XYZ_JSON { PropertyName = "Romulus", PropertyValue = "123123" };
  
      return test;
  }
  </code></pre>
  
  <p>Will result with an http response of something like:</p>
  
  <p>200 OK
  <code>{"PropertyName":"Romulus", "PropertyValue":"123123"}</code></p>
  
  <p>This whole class to JSON design pattern is cool and all, but it's not helpful and actually makes things much worse when trying to return a class as a JSON object with many classes within it such as:</p>
  
  <pre><code>public class XYZ_JSON
  {
      public string PropertyName { get; set; }
      public string PropertyValue { get; set; }
      public List&lt;ComplexObject&gt; objects { get; set; } // &lt;- do not want
  }
  </code></pre>
  
  <p>The JSON response object above isn't that complex, but for what I'm trying to accomplish I'll have to put a list of classes within a list of classes within a list of classes, and I can't develop it in this awkward way unless I spend a week on it which is just ridiculous.</p>
  
  <p>I need to be able to return a JSON response in this kind of fashion:</p>
  
  <pre><code>// GET: api/ReturnJSON
  public JSON_Response Get(string id)
  {
      // do some SQL querying here to grab the model or what have you.
  
      if (somethingGoesWrong = true)
      return {"result":"fail"}
      else
      return {"result":"success","value":"some value goes here"}
  }
  </code></pre>
  
  <p>The design pattern above is what I'm trying to accomplish with ASP.NET web API, a very simply way to return a semi-hard coded JSON response object which would allow me to return very unique and dynamic responses from a single URI. There's going to be many use cases where a list of up to 8 completely unique Class objects will be returned. </p>
  
  <p>Also, If what I'm trying to accomplish is the backwards way of doing things than that's fine. I've released a very successful and stable iOS application with a flawless Django backend server handling things this way perfectly without any issues.</p>
  
  <p>Can someone explain to me how I can return a simple hard coded JSON response using the ASP.NET web API?</p>
  
  <p>Thanks! </p>