簡體   English   中英

ASP.NET 內核 3.1 | 參數 model 總是 null 與 controller 動作方法中的大表格數據

[英]ASP.NET Core 3.1 | Argument model always null with large form data in controller action method

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.

如果我減少數據量,那么它可以正常工作,但是對於大量數據,它總是 null。

以下鏈接已經嘗試過

在 Asp.Net 核心中增加上傳文件的大小

新的默認 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);
                }
            });
        };

這是我的 controller 操作方法:

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

提前致謝

在 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>

您應該在 StartUp class 中設置 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM