dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.35k stars 9.99k forks source link

MultipartBodyLengthLimit is not working in ASP.NET Core Version >= 2.0 #18162

Closed TanvirArjel closed 4 years ago

TanvirArjel commented 4 years ago

I am trying to upload a file with size more than 30 MB and its throwing the following exception:

HTTP Error 404.13 - Not Found The request filtering module is configured to deny a request that exceeds the request content length.

Then I have configured as follows:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<FormOptions>(x =>
        {
            x.ValueLengthLimit = int.MaxValue;
            x.MultipartBodyLengthLimit = int.MaxValue;
            x.MultipartHeadersLengthLimit = int.MaxValue;
        });

        services.AddControllersWithViews();
    }

But still not working!

I have also tried as follows:

    [HttpPost]
    [ValidateAntiForgeryToken]
    [RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
    public ActionResult Create(Employee employee)
    {
        if (ModelState.IsValid)
        {
            return View();
        }

        return View(employee);
    }

And

    [HttpPost]
    [ValidateAntiForgeryToken]
    [DisableRequestSizeLimit]
    public ActionResult Create(Employee employee)
    {
        if (ModelState.IsValid)
        {
            return View();
        }

        return View(employee);
    }

But still nothing is working!

Above configurations have been tried in ASP.NET Core 2.1,2.2,3.0 and 3.1 versions but did not work with any of them.

halter73 commented 4 years ago

You're probably hitting Kestrel's default 30 MB (~28.6 MiB) max request body size limit introduced in 2.0.0. You can change it in MVC with the RequestSizeLimitAttribute or disable it entirely with the DisableRequestSizeLimitAttribute.