I have the following controller method which returns a byte array.
public async Task<HttpResponseMessage> Get()
{
var model = new byte[] { 1, 2, 3 };
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new StreamContent(new MemoryStream(model));
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
I think this is an older way of implementing this functionality with web api. Is there a more "modern" version?
For example, is returning a Task<IHttpActionResult>
the preferred way now? And if so, what would be the code to return the byte array from above?
As the comment pointed out. I dont think there is a new way to do this. But if you would like to return an
IHttpActionResult
instead, there is a base method that returns aResponseMessageResult
:Also, to return binary data in AspNetCore WebApi2 if anyone needs it: