zoukankan      html  css  js  c++  java
  • .net core 问题:413 Request Entity Too Large nginx

    https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core

    The other answers solve the IIS restriction. However, as of ASP.NET Core 2.0, Kestrel serveralso imposes its own default limits.

    Github of KestrelServerLimits.cs

    Announcement of request body size limit and solution (quoted below)

    MVC Instructions

    If you want to change the max request body size limit for a specific MVC action or controller, you can use the RequestSizeLimit attribute. The following would allow MyAction to accept request bodies up to 100,000,000 bytes.

    [HttpPost]
    [RequestSizeLimit(100_000_000)]
    public IActionResult MyAction([FromBody] MyViewModel data)
    {
    

    [DisableRequestSizeLimit] can be used to make request size unlimited. This effectively restores pre-2.0.0 behavior for just the attributed action or controller.

    Generic Middleware Instructions

    If the request is not being handled by an MVC action, the limit can still be modified on a per request basis using the IHttpMaxRequestBodySizeFeature. For example:

    app.Run(async context =>
    {
        context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000;
    

    MaxRequestBodySize is a nullable long. Setting it to null disables the limit like MVC's [DisableRequestSizeLimit].

    You can only configure the limit on a request if the application hasn’t started reading yet; otherwise an exception is thrown. There’s an IsReadOnly property that tells you if the MaxRequestBodySizeproperty is in read-only state, meaning it’s too late to configure the limit.

    Global Config Instructions

    If you want to modify the max request body size globally, this can be done by modifying a MaxRequestBodySize property in the callback of either UseKestrel or UseHttpSysMaxRequestBodySize is a nullable long in both cases. For example:

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

    or

    .UseHttpSys(options =>
    {
        options.MaxRequestBodySize = 100_000_000;






    You must configure two things:

    In your Program.cs

    public static IWebHost BuildWebhost(string[] args) => 
       WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>()
          .UseKestrel(options => {
               options.Limits.MaxRequestBodySize = null; // or a given limit
          })
         .Build();

    In your Startup.cs in the ConfigureService method

    services.Configure<FormOptions>(options => options.MultipartBodyLengthLimit = long.MaxValue); // or other given limit

    Also change your controller endpoint to use [FromForm]

    public IActionResult Upload([FromForm] IEnumerable<IFormFile> files)... // name must be same as name attribute of your multipart form

    Now ASP.NET Core will do the work and inject the files from the form as sequence.

    Edit:

    I created an example that can be cloned from github:

    git clone https://github.com/Birdperson90/example-fileupload-aspnet-core.git
    cd example-fileupload-aspnet-core
    dotnet restore
    dotnet run --project src/file-upload-api/file-upload-api.csproj

    Then navigate to http://localhost:5000/index.html and try uploading huge files.

    413 error with Kubernetes and Nginx ingress controller

    You can use the annotation nginx.ingress.kubernetes.io/proxy-body-size to set the max-body-size option right in your Ingress object instead of changing a base ConfigMap.

    Here is the example of usage:

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: my-app
      annotations:
        nginx.ingress.kubernetes.io/proxy-body-size: "50m"
    ...
     
  • 相关阅读:
    基于Modelsim的直方图线性拉伸
    基于Modelsim的直方图统计算法仿真
    基于Modelsim的均值滤波仿真
    基于Modelsim的直方图均衡化算法仿真
    基于FPGA的RGB图像转灰度图像算法实现
    基于Modelsim的视频捕获模拟仿真
    基于Modelsim的视频流仿真
    【MSSQL】执行大sql文件-内容乱码处理
    【WINForm】C#应用程序图标设置问题
    【dotNet Core】Swagger下简单的给WebApi分组
  • 原文地址:https://www.cnblogs.com/panpanwelcome/p/9100102.html
Copyright © 2011-2022 走看看