title: MVC: How to Return a String as JSON
body: <p>In an effort to make a progress reporting process a little more reliable and decouple it from the request/response, I am performing the processing in a Windows Service and persisting the intended response to a file.  When the client starts polling for updates, the intention is that the controller returns the contents of the file, whatever they are, as a JSON string.</p>
  
  <p>The contents of the file are pre-serialized to JSON.  This is to ensure that there is nothing standing in the way of the response.  No processing needs to happen (short of reading the file contents into a string and returning it) to get the response.</p>
  
  <p>I initially though this would be fairly simple, but it is not turning out to be the case.</p>
  
  <p>Currently my controller method looks thusly:</p>
  
  <h1>Controller</h1>
  
  <h2>Updated</h2>
  
  <pre><code>[HttpPost]
  public JsonResult UpdateBatchSearchMembers()
  {
      string path = Properties.Settings.Default.ResponsePath;
      string returntext;
      if (!System.IO.File.Exists(path))
          returntext = Properties.Settings.Default.EmptyBatchSearchUpdate;
      else
          returntext = System.IO.File.ReadAllText(path);
  
      return this.Json(returntext);
  }
  </code></pre>
  
  <p>And Fiddler is returning this as the raw response</p>
  
  <pre><code>HTTP/1.1 200 OK
  Server: ASP.NET Development Server/10.0.0.0
  Date: Mon, 19 Mar 2012 20:30:05 GMT
  X-AspNet-Version: 4.0.30319
  X-AspNetMvc-Version: 3.0
  Cache-Control: private
  Content-Type: application/json; charset=utf-8
  Content-Length: 81
  Connection: Close
  
  "{\"StopPolling\":false,\"BatchSearchProgressReports\":[],\"MemberStatuses\":[]}"
  </code></pre>
  
  <h1>AJAX</h1>
  
  <h2>Updated</h2>
  
  <p>The following will likely be changed later, but for now this was working when I was generating the response class and returning it as JSON like a normal person.</p>
  
  <pre><code>this.CheckForUpdate = function () {
  var parent = this;
  
  if (this.BatchSearchId != null &amp;&amp; WorkflowState.SelectedSearchList != "") {
      showAjaxLoader = false;
      if (progressPending != true) {
          progressPending = true;
          $.ajax({
              url: WorkflowState.UpdateBatchLink + "?SearchListID=" + WorkflowState.SelectedSearchList,
              type: 'POST',
              contentType: 'application/json; charset=utf-8',
              cache: false,
              success: function (data) {
                  for (var i = 0; i &lt; data.MemberStatuses.length; i++) {
                      var response = data.MemberStatuses[i];
                      parent.UpdateCellStatus(response);
                  }
                  if (data.StopPolling = true) {
                      parent.StopPullingForUpdates();
                  }
                  showAjaxLoader = true;
              }
          });
          progressPending = false;
      }
  }
  </code></pre>