解決.NET Core Ajax請求后臺傳送參數過大請求失敗問題
今天在項目上遇到一個坑,
在.Net Core中通過ajax向mvc的controller傳遞對象時,控制器(controller)的方法一直沒有進去,百思不得其解,
后面把傳遞的參數打印出來發(fā)現傳遞的參數比較大,有2.4M的數據,如下圖:

后面跟蹤項目發(fā)現web.config和Startup.cs里面沒有設置數據傳輸大小(至于默認的數據大小是多少就沒深究了),
到這里就明了了,就只要在web.config和Startup.cs里面設置一下就好了,注意設置方法和.Net Formwork不同,具體操作如下:
web.config里面添加,添加位置如圖:
<requestFiltering> <!-- 1GB--> <requestLimits maxAllowedContentLength="1073741822" /> </requestFiltering>

Startup.cs里面的ConfigureServices方法里面添加,添加位置如圖:
/** begin xiongze 2021-03-08**************///上傳文件大小限制Kestrel設置services.Configure(options =>? ? ? ? ? ? {
? ? ? ? ? ? ? ? // Set the limit to 256 MBoptions.Limits.MaxRequestBodySize =268435456;
? ? ? ? ? ? });
//上傳文件大小限制IIS設置services.Configure(options =>? ? ? ? ? ? {
? ? ? ? ? ? ? ? // This lambda determines whether user consent for non-essential cookies is needed for a given request.options.CheckConsentNeeded = context =>true;
? ? ? ? ? ? ? ? options.MinimumSameSitePolicy = SameSiteMode.None;
? ? ? ? ? ? });
//解決文件上傳Multipart body length limit 134217728 exceededservices.Configure(x =>? ? ? ? ? ? {
? ? ? ? ? ? ? ? x.ValueLengthLimit =int.MaxValue;
? ? ? ? ? ? ? ? x.MultipartBodyLengthLimit =int.MaxValue;
? ? ? ? ? ? ? ? x.MemoryBufferThreshold =int.MaxValue;
? ? ? ? ? ? });
? ? ? ? ? ? /** end xiongze 2021-03-08**************/

添加好后就可以運行了,如圖,終于進控制器(controller)的方法斷點了
