1

I am replacing a HttpHandler with a middleware service. I have all the code working except for returning the actual image. All the existing samples are for asp.net Core (or earlier) , but with asp.net core 1.1 the response object has change?

public async Task Invoke(HttpContext context)
{
      var mediaType = new MediaTypeHeaderValue("image/jpeg");
      mediaType.Encoding = System.Text.Encoding.UTF8;
      context.Response.ContentType = mediaType.ToString();
      byte[] results = some process that generates a byte array
      Stream stream = new MemoryStream(results);
      context.Response.Body = stream;
      await _next.Invoke(context);
 }

So how do we attach the byte array to the response object?

1
  • Thanks this was the exact solution to my problem Commented Dec 1, 2016 at 2:04

1 Answer 1

1

There is couple of methods which you can use on .NET Core 1.1:

httpContext.Response.Body.WriteAsync([BUFFER], [OFFSET], [COUNT]);
httpContext.Response.Body.Write([BUFFER], [OFFSET], [COUNT]);
httpContext.Response.Body.WriteByte([BYTE]);
httpContext.Response.WriteAsync([TEXT])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.