4

As I am developing a web application in ASP.NET Core 3.1 and I am passing large form data using ajax into my controller and it is always null.

If I decrease the amount of data, then it works fine, but with large amounts of data, it's always null.

Below links already tried

Increase upload file size in Asp.Net core

New default 30 MB (~28.6 MiB) max request body size limit

This is my Ajax call

publicObject.PostRequest = function (url, data, onSuccess, onError, _withoutLoader) {
            $.ajax({
                url: _url,
                type: "POST",
                headers: {
                    'RequestVerificationToken': $('input:hidden[name="__RequestVerificationToken"]').val()
                },
                cache: false,
                data: _data,
                success: function (_data) {
                    //CFD.Loader.hide();
                    _onSuccess(_data);
                },
                error: function (result, textStatus, _xhr) {
                    CFD.Loader.hide();
                    if (_result.status == 401) {
                        CFD.User.GetLoginModal('loginsection');
                    }
                    _onError(_result);
                }
            });
        };

Here is my controller action method:

[HttpPost]
[RequestFormLimits(MultipartBodyLengthLimit = int.MaxValue)]
[RequestSizeLimit( int.MaxValue)]
public async Task<IActionResult> GetProducts(VMProductFilter model)
{
    List<VMProduct> resultModel = null;

    try
    {
        resultModel = await GetAllProducts(model);
    }
    catch (Exception ex)
    {
        ex.Log();
    }

    return PartialView("_ProductGrid", resultModel);
}

Thanks In advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mansinh
  • 1,365
  • 4
  • 19
  • 47

2 Answers2

3

Configure request limits in the Startup.cs.

   public void ConfigureServices(IServiceCollection services)
    {
        //...

        services.Configure<FormOptions>(x =>
        {
            //x.ValueLengthLimit = Settings.ValueLenLimit;
            x.MultipartBodyLengthLimit = Settings.MulBodyLenLimit;
            //x.MemoryBufferThreshold = Settings.MemoryBufferThreshold;
        });

        //...
    }

If IIS is used, it also needs to be configured in web.config:

<system.webServer>
  <security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741822" /><!-- 1GB-->
    </requestFiltering>
  </security>
</system.webServer>
Zhan
  • 31
  • 1
2

You should set FormOptions in your StartUp class:

public void ConfigureServices(IServiceCollection services)
{
    // Set limits for form options, to accept big data
    services.Configure<FormOptions>(x =>
    {
        x.BufferBody = false;
        x.KeyLengthLimit = 2048; // 2 KiB
        x.ValueLengthLimit = 4194304; // 32 MiB
        x.ValueCountLimit = 2048;// 1024
        x.MultipartHeadersCountLimit = 32; // 16
        x.MultipartHeadersLengthLimit = 32768; // 16384
        x.MultipartBoundaryLengthLimit = 256; // 128
        x.MultipartBodyLengthLimit = 134217728; // 128 MiB
    });

    ...

} // End of the ConfigureServices method