简体   繁体   English

413 请求实体太大 - Web API

[英]413 request entity too large - Web API

I'm running into a 413 issue while trying to send data from my web application (.netfx 4.6.1) to my web api (.net core 3.1).我在尝试将数据从我的 Web 应用程序 (.netfx 4.6.1) 发送到我的 Web api (.net core 3.1) 时遇到了 413 问题。 In the code below, I send a list over containing byte data of images along with additional data needed to build a file.在下面的代码中,我发送了一个列表,其中包含图像的字节数据以及构建文件所需的附加数据。 The expected output is to return a byte array containing the new file.预期的输出是返回一个包含新文件的字节数组。 Unfortunately when sending the request I receive an error: Response status code does not indicate success: 413 (Request Entity Too Large).不幸的是,在发送请求时,我收到一个错误:响应状态代码不表示成功:413(请求实体太大)。

The error only seems to occur when the file is large to begin with, which makes sense.该错误似乎仅在文件很大时才会发生,这是有道理的。 The research I've done seems to point to settings in IIS, the main ones being maxAllowedContentLength, maxRequestLength, and uploadReadAheadSize.我所做的研究似乎指向 IIS 中的设置,主要是 maxAllowedContentLength、maxRequestLength 和 uploadReadAheadSize。 I've tried increasing these values to ones more suited to this process but nothing seems to work.我尝试将这些值增加到更适合此过程的值,但似乎没有任何效果。 I've adjusted them for both the web application and the web api, as I was not sure which one was causing the problem.我已经针对 web 应用程序和 web api 调整了它们,因为我不确定是哪个导致了问题。

Where does the problem lie?问题出在哪里? In the application, the API, or both?在应用程序中,API,还是两者兼而有之? Is there an additional setting I'm missing to allow an increased size?我是否缺少允许增加尺寸的其他设置? Is there an issue with how I'm sending the request?我发送请求的方式有问题吗? Any help is appreciated.任何帮助表示赞赏。

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

Can you check that attribute https://github.com/aspnet/Announcements/issues/267 ?您可以检查该属性https://github.com/aspnet/Announcements/issues/267吗? Using使用

[RequestSizeLimit(100_000_000)] 

on your controller entry point, or more globally setting it this way:在您的控制器入口点上,或更全局地以这种方式设置它:

.UseKestrel(options =>
{
    options.Limits.MaxRequestBodySize = null;

EDIT: article from MS: https://dotnet.microsoft.com/download/dotnet-core编辑:来自 MS 的文章: https : //dotnet.microsoft.com/download/dotnet-core

I think the problem is on the server.我认为问题出在服务器上。 The server is terminating the request because it exceeds it's configured maximum allowable request size.服务器正在终止请求,因为它超过了它配置的最大允许请求大小。

Which server are you using?您使用的是哪个服务器? For Nginx users, the directive which determines what the allowable HTTP request size can be is client_max_body_size , the default maximum allowable request size is 1MB.对于 Nginx 用户,决定允许的 HTTP 请求大小的指令是client_max_body_size ,默认的最大允许请求大小为 1MB。 The limit in Apache is set via the LimitRequestBody directive and defaults to 0 (meaning unlimited) to 2147483647 (2GB). Apache 中的限制是通过 LimitRequestBody 指令设置的,默认为 0(表示无限制)到 2147483647 (2GB)。

Check out this article on how you can fix it if you are using any of those two servers.如果您使用这两个服务器中的任何一个,请查看这篇文章,了解如何修复它。

Below changes worked for me以下更改对我有用

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

create web.config file and add following configuration创建 web.config 文件并添加以下配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="2147483648" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM