Guy Gallant
Guy Gallant

Reputation: 339

ASP.Net Core 1.1 MiddleWare response

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?

Upvotes: 1

Views: 1237

Answers (1)

Dawid Rutkowski
Dawid Rutkowski

Reputation: 2756

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])

Upvotes: 1

Related Questions