413 请求实体太大 - Web API

Dri*_*Far 6 asp.net iis asp.net-web-api http-status-code-413 asp.net-core

我在尝试将数据从 Web 应用程序 (.netfx 4.6.1) 发送到我的 Web api (.net core 3.1) 时遇到 413 问题。在下面的代码中,我发送了一个列表,其中包含图像的字节数据以及构建文件所需的附加数据。预期输出是返回包含新文件的字节数组。不幸的是,在发送请求时,我收到错误:响应状态代码未指示成功:413(请求实体太大)。

该错误似乎仅在文件一开始就很大时才会发生,这是有道理的。我所做的研究似乎指向 IIS 中的设置,主要是 maxAllowedContentLength、maxRequestLength 和 uploadReadAheadSize。我尝试将这些值增加到更适合此过程的值,但似乎没有任何效果。我已经针对 Web 应用程序和 Web api 调整了它们,因为我不确定是哪一个导致了问题。

问题出在哪里?在应用程序、API 或两者中?我是否缺少允许增加尺寸的附加设置?我发送请求的方式有问题吗?任何帮助表示赞赏。

    public static async Task<byte[]> CreatePdfFromImageFilesAsync(List<ImageFile> imageFiles)
    {
        var list = new List<dynamic>();
        foreach (var item in imageFiles)
        {
            list.Add(new
            {
                Data = Convert.ToBase64String(item.Bytes),
                PageOrder = item.PageOrder,
                Rotation = item.Rotation,
                Type = "PDF"
            });
        }

        var response = _client.PostAsJsonAsync($"{FileCreatorAPI}/api/files/CreateFileFromMultiple", list).Result;
        var result = response.EnsureSuccessStatusCode();
        var bytes = await result.Content.ReadAsAsync<byte[]>();
        return bytes;
    }
Run Code Online (Sandbox Code Playgroud)

Nil*_*ant 7

以下更改对我有用

                // If using Kestrel:
                .Configure<KestrelServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                    //options.Limits.MaxRequestBodySize = null; --did not worked
                    options.Limits.MaxRequestBodySize = int.MaxValue;
                })
                // If using IIS:
                .Configure<IISServerOptions>(options =>
                {
                    options.AllowSynchronousIO = true;
                    //options.MaxRequestBodySize = null;
                    options.MaxRequestBodySize = int.MaxValue;
                });
Run Code Online (Sandbox Code Playgroud)

创建 web.config 文件并添加以下配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)


Dab*_*oul 4

您可以检查该属性https://github.com/aspnet/Announcements/issues/267吗?使用

[RequestSizeLimit(100_000_000)] 
Run Code Online (Sandbox Code Playgroud)

在你的控制器入口点上,或者更全局地这样设置:

.UseKestrel(options =>
{
    options.Limits.MaxRequestBodySize = null;
Run Code Online (Sandbox Code Playgroud)