How to specify ContentType for Web API controller method

You’ll have to change the return type of the method to HttpResponseMessage, then use Request.CreateResponse:

// GET api/Audit/CSV
[HttpGet, ActionName("CSV")]
public HttpResponseMessage Csv(Guid sessionId, DateTime a, DateTime b, string predicate)
{
    var result = new StringBuilder();

    //build a string

    var res = Request.CreateResponse(HttpStatusCode.OK);
    res.Content = new StringContent(result.ToString(), Encoding.UTF8, "text/csv");

    return res;
}

Leave a Comment