3

I need to serve chunked transfer encoding data using an ApiController. Because I do not have access to the HttpContext or the HttpRequest, I'm a bit lost as to where to write to the response and where to flush it.

The setup looks like:

public class MyController : ApiController
{
   [Route("testing")]
   [HttpGet]
   public string Get()
   {
       ...
       return <response object ot HttpResponseMessage
   }
}

I guess I might be using the wrong base classes/framework/concept? Thanks so much!

G.L.P
  • 7,119
  • 5
  • 25
  • 41
Miquel
  • 15,405
  • 8
  • 54
  • 87

1 Answers1

1

You do have access to the Context and the Request. You need access to the Response though:

public string Get()
{
    ActionContext.Response.Headers.TransferEncodingChunked = true;
    // ...
}
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 1
    Yes, but you need to be careful because there is a bug in certain versions of the framework when using chunking with proxies. See http://stackoverflow.com/questions/16736233/web-api-as-a-proxy-and-chunked-transfer-encoding and https://aspnetwebstack.codeplex.com/workitem/1124 . This was apparently fixed in the most recent version, but at least one person is reporting the problem still exists.. Not sure about ASP.NET 5 though. – Erik Funkenbusch Jul 14 '15 at 14:19
  • I'm actually getting a null `ActionContext.Response`. I can however create a new HttpResponseMessage and return it – Miquel Jul 14 '15 at 16:36