将大文件上传到 Azure 中的 dotnetcore 应用程序失败 (404)

Uploading big files to dotnetcore app in Azure fails (404)

我无法上传大文件,我不确定它是否仍然是大小限制或超时问题。

在控制器端点上,我尝试了我找到的所有属性(一次)

        [HttpPost("[action]")]
        [DisableRequestSizeLimit]
        [RequestFormLimits(MultipartBodyLengthLimit = long.MaxValue, BufferBodyLengthLimit = long.MaxValue)]
        [RequestSizeLimit(int.MaxValue)]
        public async Task UploadForm()

在 'ConfigureServices' 期间,我还设置了这个:

      services.Configure<FormOptions>(options =>
        {
            options.MemoryBufferThreshold = int.MaxValue;
            options.ValueLengthLimit = int.MaxValue;
            options.ValueCountLimit = int.MaxValue;
            options.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
        });

但是我上传了一部分文件后仍然出现 404 错误(30 MB 已经太多了)。

然后我什至尝试使用以下代码设置 kestrel,但应用甚至无法启动 (502)

.UseKestrel((KestrelServerOptions o) =>
            {
                o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(120);
                o.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(120);
                o.Limits.MaxRequestBodySize = null;
            })

看看这个Offcial doc

解法:

更改 maxAllowedContentLength 的值。

在 Web.config 中添加这些代码(在 Kudu 的 site/wwwroot 下):

<configuration>
   <system.webServer>
      <security>
         <requestFiltering>
            <requestLimits maxAllowedContentLength="<valueInBytes>"/>
         </requestFiltering>
      </security>
   </system.webServer>
</configuration>

无需重启即可正常工作。