Last post Sep 09, 2016 03:38 AM by OzBobWa
Nov 12, 2014 06:20 AM|politech|LINK
I would like to ask if it is necessary to override UseBufferedInputStream method in Custom WebHostBufferPolicySelector, where base on rout i can turon on/off buffering.
What i mean is that I am uploading large file through API and I want to use streaming, so I wont use that much of memory, I heard that by default WEB API is buffering request and i want to avoid it, so according to some materials What i have done is:
public class NoBufferWebHostPolicySelector : WebHostBufferPolicySelector
public override bool UseBufferedInputStream(object hostContext)
var context = hostContext as HttpContextBase;
if (context != null)
if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "upload", StringComparison.InvariantCultureIgnoreCase))
public override bool UseBufferedOutputStream(HttpResponseMessage response)
protected void Application_Start()
GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferWebHostPolicySelector());
But then UseBufferedInputStream method is called, context.Request.RequestContext.RouteData is always empty.
So now i am wondering if this is really thing I need to do, and if so, why I have context.Request.RequestContext.RouteData empty in this method.
Anyone can help me?
Nov 13, 2014 03:30 AM|LemonStreet|LINK
About Dealing with large files in ASP.NET Web API:
Nov 13, 2014 05:52 AM|politech|LINK
Unfortunately this is not answer to my question.
Sep 09, 2016 03:38 AM|OzBobWa|LINK
Accessing RouteData is not possible, although the type of the hostContext in your code is System.Web.HttpContextWrapper, there will be no RouteData **yet**, it get's populated further down the 'pipeline'.
It may be necessary to do a Url text search e.g. hostContext.Request.RawUrl.Contains("/upload/");//with the slashes
more ideas can be found on p123 of 'ASP.NET Web API 2 Recipes: A Problem-Solution Approach' (https://books.google.com.br/books?id=7aE8BAAAQBAJ&pg=PA123&lpg=PA123&dq=override+WebHostBufferPolicySelector&source=bl&ots=fvV5SWVsJG&sig=I5t4MBOXdPbgFV4mBCI_kWLWkRw&hl=en-AU&sa=X&ei=oheIVev2N-TgsASS2IDgCQ&ved=0CD0Q6AEwAw#v=onepage&q=override%20WebHostBufferPolicySelector&f=false)
or find out away to call the contstructor in MVC/API base, to populate the RouteData using your hostContext....