【问题标题】:ASP.NET Core 3.1 | Argument model always null with large form data in controller action methodASP.NET 核心 3.1 |控制器操作方法中的参数模型始终为空,且表单数据较大
【发布时间】:2020-08-01 09:02:29
【问题描述】:

当我在 ASP.NET Core 3.1 中开发 Web 应用程序时,我正在使用 ajax 将大型表单数据传递到我的控制器中,并且它始终为空。

如果我减少数据量,那么它可以正常工作,但是对于大量数据,它总是为空。

以下链接已经尝试过

Increase upload file size in Asp.Net core

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

这是我的 Ajax 调用

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);
                }
            });
        };

这是我的控制器操作方法:

[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);
}

提前致谢

【问题讨论】:

  • 尝试使用 Gzip 压缩表单数据并在服务器上解压缩。
  • 您的数据有多大?我尝试了 50MB 的多部分表单数据,结果完美无缺。

标签: c# asp.net-core-mvc


【解决方案1】:

在 Startup.cs 中配置请求限制。

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

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

        //...
    }

如果使用IIS,还需要在web.config中进行配置:

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

【讨论】:

    【解决方案2】:

    您应该在 StartUp 类中设置 FormOptions:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2016-05-12
      • 2013-01-18
      • 1970-01-01
      • 2014-12-21
      • 2017-12-31
      • 1970-01-01
      相关资源
      最近更新 更多