iis 异常错误:请求正文太大

cfh9epnr  于 2022-11-12  发布在  其他
关注(0)|答案(5)|浏览(145)

我正在尝试将一个100MB的电影上传到我的ASP.NET核心应用程序。
我已经在我的操作上设置了这个属性:

[RequestSizeLimit(1_000_000_000)]

我还更改了Web.config文件,以包括:

<security>
  <requestFiltering>
    <!-- This will handle requests up to 700MB (CD700) -->
    <requestLimits maxAllowedContentLength="737280000" />
  </requestFiltering>
</security>

换句话说,我已经告诉IIS允许最大700MB的文件,我还告诉ASP.NET核心允许接近1GB的文件。
但我还是会出错。而且我找不到答案。有什么想法吗?
使用这些配置,我可以通过30MB的默认大小。我可以上传50或70兆字节的文件。

tuwxkamq

tuwxkamq1#

我想你只需要:[禁用请求大小限制]
下面是一个解决方案,我将Zip文件和附加的表单数据上传到运行.Net Core 3的API

// MultipartBodyLengthLimit  was needed for zip files with form data.
// [DisableRequestSizeLimit] works for the KESTREL server, but not IIS server 
// for IIS: webconfig... <requestLimits maxAllowedContentLength="102428800" />
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)] 
[DisableRequestSizeLimit] 
[Consumes("multipart/form-data")] // for Zip files with form data
[HttpPost("MyCustomRoute")]
public IActionResult UploadZippedFiles([FromForm] MyCustomFormObject formData)
{ }
s4n0splo

s4n0splo2#

注意:这是我在将应用程序从www.example.com core 2.1迁移到3.0时遇到的问题asp.net
为了在www.example.com core 3.0中解决这个问题asp.net,我已经修改了我的program.cs,以修改最大请求正文大小,如下所示。

public class Program
{
    public static void Main(string[] args)
    {
       CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
       return WebHost.CreateDefaultBuilder(args)
            .ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = 737280000;
            })
            .UseStartup<Startup>();
        }
    }
}

我的意思是,我刚刚添加了ConfigureKestrel部分,并在我的action方法[RequestSizeLimit(737280000)]上添加了一个属性,如下所示

[HttpPost]
[RequestSizeLimit(737280000)]
[Route("SomeRoute")]
public async Task<ViewResult> MyActionMethodAsync([FromForm]MyViewModel myViewModel)
{
   //Some code
   return View();
}

我的应用程序再次开始正常运行,没有抛出BadHttpRequestException: Request body too large
参考:https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.0#kestrel-maximum-request-body-size

jvidinwx

jvidinwx3#

对我来说(ASP.NET core 3.1),解决方案是在Startup.csConfigureServices方法中添加以下行:

// 200 MB
    const int maxRequestLimit = 209715200;
    // If using IIS
    services.Configure<IISServerOptions>(options =>
    {
        options.MaxRequestBodySize = maxRequestLimit;
    });
    // If using Kestrel
    services.Configure<KestrelServerOptions>(options =>
    {
        options.Limits.MaxRequestBodySize = maxRequestLimit;
    });
    services.Configure<FormOptions>(x =>
    {
        x.ValueLengthLimit = maxRequestLimit;
        x.MultipartBodyLengthLimit = maxRequestLimit;
        x.MultipartHeadersLengthLimit = maxRequestLimit;
    });

和编辑web.config

<system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="209715200" />
        </requestFiltering>
      </security>
    </system.webServer>
vwkv1x7d

vwkv1x7d4#

我使用web.config进行配置(而我们的API托管在IIS中):

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

但是现在我们将我们的api移到linux容器并使用Kestrel。然后我这样配置它:

.ConfigureWebHostDefaults(webBuilder =>
{
    webBuilder
    .ConfigureKestrel(serverOptions =>
    {
        serverOptions.Limits.MaxRequestBodySize = 157286400;
    })
    .UseStartup<Startup>();
})

157286400 = 150兆字节;

相关问题