title: Disabling Model Binding for Post Requests
body: <p>I am trying to implement the file streaming example from <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads</a></p>
  
  <p>As part of that, I have implemented the filter:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
  public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
  {
      public void OnResourceExecuting(ResourceExecutingContext context)
      {
          var formValueProviderFactory = context.ValueProviderFactories
              .OfType&lt;FormValueProviderFactory&gt;()
              .FirstOrDefault();
          if (formValueProviderFactory != null)
          {
              context.ValueProviderFactories.Remove(formValueProviderFactory);
          }
  
          var jqueryFormValueProviderFactory = context.ValueProviderFactories
              .OfType&lt;JQueryFormValueProviderFactory&gt;()
              .FirstOrDefault();
          if (jqueryFormValueProviderFactory != null)
          {
              context.ValueProviderFactories
                  .Remove(jqueryFormValueProviderFactory);
          }
      }
  
      public void OnResourceExecuted(ResourceExecutedContext context)
      {
      }
  }
  </code></pre>
  
  <p>I am getting an IOException</p>
  
  <pre><code>System.IO.IOException: Unexpected end of Stream, the content may have 
  already been read by another component. 
  at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.
  &lt;ReadAsync&gt;d__36.MoveNext()
  </code></pre>
  
  <p>This is... vexing.  I've found this question 
  <a href="https://stackoverflow.com/questions/36444457/unexpected-end-of-stream-at-microsoft-aspnetcore-webutilities-multipartreaderstr">Unexpected end of stream at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream</a></p>
  
  <p>To which the answer is basically "Implement the DisableFormValueModelBinding Attribute".  Obviously, that isn't working.  </p>
  
  <p>My Razor code is </p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data" asp-controller="FileStore" asp-action="LargeUpload"&gt;
      &lt;div class="form-group"&gt;
          &lt;!--&lt;div class="col-md-10"&gt;
              &lt;p&gt;Please describe your file&lt;/p&gt;
              &lt;input type="text" name="description"/&gt;
          &lt;/div&gt;--&gt;
          &lt;div class="col-md-10"&gt;
              &lt;p&gt;Upload one or more files using this form:&lt;/p&gt;
              &lt;input type="file" name="files" multiple/&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;div class="col-md-10"&gt;
              &lt;input type="submit" value="Large Upload" /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And my Controller is:</p>
  
  <pre><code>[HttpPost]
  [DisableFormValueModelBinding]
  [ValidateAntiForgeryToken]
  public async Task&lt;IActionResult&gt; LargeUpload()
  {
      if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType))
      {
          return BadRequest($"Expected a multipart request, but got {Request.ContentType}");
      }
  
      //// Used to accumulate all the form url encoded key value pairs in the 
      //// request.
      //var formAccumulator = new KeyValueAccumulator();
      //string targetFilePath = null;
  
      var boundary = MultipartRequestHelper.GetBoundary(
          MediaTypeHeaderValue.Parse(Request.ContentType),
          _defaultFormOptions.MultipartBoundaryLengthLimit);
      var reader = new MultipartReader(boundary, HttpContext.Request.Body);
  
      var section = await reader.ReadNextSectionAsync();
  }
  </code></pre>
  
  <p>Does anyone know if I've missed something or if the tutorials are out of date?
  I'm running VS2017 community with ASP.NET Core 1.1.2</p>