ASP.NET Core 3.1 | ASP.NET Core 3.1 在控制器操作方法中,参数模型始终为空且具有大格式数据

Man*_*inh 5 c# asp.net-core-mvc

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

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

下面的链接已经尝试过

增加Asp.Net core中上传文件的大小

新的默认最大请求正文大小限制为 30 MB (~28.6 MiB)

这是我的 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);
                }
            });
        };
Run Code Online (Sandbox Code Playgroud)

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

[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);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

小智 5

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

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

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

        //...
    }
Run Code Online (Sandbox Code Playgroud)

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

<system.webServer>
  <security>
    <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741822" /><!-- 1GB-->
    </requestFiltering>
  </security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)