In web api 2 we used to do this to get a response with string content:
var response = Request.CreateResponse(HttpStatusCode.Ok);
response.Content = new StringContent("<my json result>", Encoding.UTF8, "application/json");
How can you acheive the same in ASP.NET 5 / MVC 6 without using any of the built in classes like ObjectResult?
You can write to the
Response.Body
stream directly (as theBody
is a plain oldSystem.IO.Stream
) and manually set content type:You could save yourself some trouble using some utilities from
Microsoft.AspNet.Http
:WriteAsync
for writing the string contents into the response body.MediaTypeHeaderValue
class for specifying the content type header. (It does some validations and has an API for adding extra parameters like the charset)So the same action would look like:
In case of doubt you can always have a look at the implementation of
ContentResult
and/orJsonResult
.