MVC6 Web Api - Return Plain Text
<p>I have looked at other similar questions (such as this one <a href="https://stackoverflow.com/questions/11581697/is-there-a-way-to-force-asp-net-web-api-to-return-plain-text">Is there a way to force ASP.NET Web API to return plain text?</a>) on SO but they all seem to address WebAPI 1 or 2, not the latest version you use with MVC6.</p>

<p>I need to return plain text on one of my Web API controllers. Only one - the others should keep returning JSON. This controller is used for dev purposes to output a list of records in a database, which will be imported into a traffic load generator. This tool takes CSV as input so I'm trying to output that (users will only have to save the content of the page).</p>

<pre class="lang-cs prettyprint-override"><code>[HttpGet]
public HttpResponseMessage AllProductsCsv()
{
    IList&lt;Product&gt; products = productService.GetAllProducts();
    var sb = new StringBuilder();
    sb.Append("Id,PartNumber");

    foreach(var product in products)
    {
        sb.AppendFormat("{0},{1}", product.Id, product.PartNumber);
    }

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StringContent(sb.ToString(), System.Text.Encoding.UTF8, "text/plain");
    return result;
}
</code></pre>

<p>Based on various searches this seems like the simplest way to proceed since I'll need this only for this one action. However when I request this, I get the following output:</p>

<pre class="lang-json prettyprint-override"><code>{
   "Version": {
      "Major": 1,
      "Minor": 1,
      "Build": -1,
      "Revision": -1,
      "MajorRevision": -1,
      "MinorRevision": -1
   },
   "Content": {
      "Headers": [
         {
            "Key": "Content-Type",
            "Value": [
               "text/plain; charset=utf-8"
            ]
         }
      ]
   },
   "StatusCode": 200,
   "ReasonPhrase": "OK",
   "Headers": [],
   "RequestMessage": null,
   "IsSuccessStatusCode": true
}
</code></pre>

<p>So it seems that MVC still tries to output JSON, and I have no idea why they'd output these values. When I debug the code step by step I can see that the content of the StringBuilder is okay and what I want to output.</p>

<p>Is there any easy way to just output a string with MVC6?</p>