Calling ReadAsFormDataAsync twice

asp.net-web-api, c#

Solution

The problem is that the underlying buffer for content is a forward-only stream that can only be read once.

Why do you need to read it twice? A little more context would help. Is it that you are reading from two separate filters?

EDIT: might try reading directly from MS_HttpContext and using that as your content stream (don't think this works in a self hosted environment).

using (var s = new System.IO.MemoryStream()) {   
  var ctx = (HttpContextBase)actionContext.Request.Properties["MS_HttpContext"];  
  ctx.Request.InputStream.Seek(0, System.IO.SeekOrigin.Begin);  
  ctx.Request.InputStream.CopyTo(s);   var body =
  System.Text.Encoding.UTF8.GetString(s.ToArray()); 
}

Problem

I'm facing a situation where I've to read the form data from incoming request in ASP.NET Web API twice (from model binder and filter). I've tried using `LoadIntoBufferAsync` but no luck. ``` // from model binder Request.Content.LoadIntoBufferAsync().Wait(); var formData = Request.Content.ReadAsFormDataAsync().Result; // from filter var formData = Request.Content.ReadAsFormDataAsync().Result; ```

Original source