Disable Model Binding for Post Requests

I am trying to implement a file streaming example from https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

As part of this, I implemented a filter:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class DisableFormValueModelBindingAttribute : Attribute, IResourceFilter
{
    public void OnResourceExecuting(ResourceExecutingContext context)
    {
        var formValueProviderFactory = context.ValueProviderFactories
            .OfType<FormValueProviderFactory>()
            .FirstOrDefault();
        if (formValueProviderFactory != null)
        {
            context.ValueProviderFactories.Remove(formValueProviderFactory);
        }

        var jqueryFormValueProviderFactory = context.ValueProviderFactories
            .OfType<JQueryFormValueProviderFactory>()
            .FirstOrDefault();
        if (jqueryFormValueProviderFactory != null)
        {
            context.ValueProviderFactories
                .Remove(jqueryFormValueProviderFactory);
        }
    }

    public void OnResourceExecuted(ResourceExecutedContext context)
    {
    }
}

      

I am getting IOException

System.IO.IOException: Unexpected end of Stream, the content may have 
already been read by another component. 
at Microsoft.AspNetCore.WebUtilities.MultipartReaderStream.
<ReadAsync>d__36.MoveNext()

      

This is ... annoying. I found this question Unexpected end of stream in Microsoft.AspNetCore.WebUtilities.MultipartReaderStream

To which the answer is basically "Implementing the DisableFormValueModelBinding attribute". Obviously this doesn't work.

My Razor code

<form method="post" enctype="multipart/form-data" asp-controller="FileStore" asp-action="LargeUpload">
    <div class="form-group">
        <!--<div class="col-md-10">
            <p>Please describe your file</p>
            <input type="text" name="description"/>
        </div>-->
        <div class="col-md-10">
            <p>Upload one or more files using this form:</p>
            <input type="file" name="files" multiple/>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-10">
            <input type="submit" value="Large Upload" />
        </div>
    </div>
</form>

      

And my controller:

[HttpPost]
[DisableFormValueModelBinding]
[ValidateAntiForgeryToken]
public async Task<IActionResult> 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();
}

      

Does anyone know if I am missing something or are out of date? I am running VS2017 community with ASP.NET Core 1.1.2

+4


source to share


1 answer


I had a similar problem because another method (our own logging engine) was reading the request body before this call was made.



0


source







All Articles