【问题标题】:asp.net core webapi frombody parameter size limitasp.net core webapi frombody 参数大小限制
【发布时间】:2021-09-05 01:48:21
【问题描述】:

我在 ASP.NET Core 3.1 中实现了一个端点来检索一个大的 JSON 对象,当这个 JSON 开始变得相对较大时我遇到了问题。我开始遇到 5~600KB 左右的问题。

对于正文小于 600~500KB 的请求,一切正常。

端点定义如下:

[DisableRequestSizeLimit]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
    // objVal is null when the post is larger than ~5~600KB
    string body = objVal.toString;
    ....
}

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
<system.web>
    <httpRuntime  maxRequestLength="1048576" />  
</system.web>

    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\ApiSLPCalendar.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
      <security>
        <requestFiltering>
          <!-- This will handle requests up to 100MB -->
          <requestLimits maxAllowedContentLength="1048576000" />
        </requestFiltering>
      </security>    
</system.webServer>

<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
</system.web.extensions>

  </location>
</configuration>

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{

            services.AddControllers().ConfigureApiBehaviorOptions(options =>
            {
                options.SuppressConsumesConstraintForFormFileParameters = true;
                options.SuppressInferBindingSourcesForParameters = true;
                options.SuppressModelStateInvalidFilter = true;
                options.SuppressMapClientErrors = true;
            });
            services.AddMvc(options =>
            {
                options.MaxModelBindingCollectionSize = int.MaxValue;
            });

            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue;
            });
....
}

你知道为什么会发生这种情况吗?

******* 编辑 *******

我尝试了几个更改,但没有一个可以正常工作。

最后,在FromBody string parameter is giving null 的讨论之后,我修改了方法,定义了一个模型,删除了泛型对象,作为参数。 前端已经修改为发送一个key="value",其中"Value"代表一个字符串化的JSON对象。

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api


    【解决方案1】:

    也许您没有达到请求大小限制,但表单大小限制。请尝试[RequestFormLimits(MultipartBodyLengthLimit = 104857600)] MultipartBodyLengthLimit 是长类型并考虑您的情况。

    如果需要上传文件大小不限:-

    services.Configure<FormOptions>(x =>
    {
        x.ValueLengthLimit = int.MaxValue;
        x.MultipartBodyLengthLimit = int.MaxValue; 
    });
    
    

    当请求到达 IIS 时,它将搜索 web.config 以检查最大上传长度。

    //clarify code
    
    <!-- 1 GB -->
     <requestLimits maxAllowedContentLength="1073741824" />
    
    //clarify code
    
    

    更新

    [HttpPost]
    [RequestFormLimits(ValueCountLimit = int.MaxValue)]
    [Route("MyTestEndpoint")]
    public void PostTest([FromBody]object objVal)
    {
        ....
        string body = objVal.toString;
        ....
    }
    
    
    
    services.Configure<FormOptions>(options =>
                {
                    options.ValueCountLimit = 10; 
                    options.ValueLengthLimit = int.MaxValue; 
                    options.MultipartBodyLengthLimit = long.MaxValue; 
                    options.MemoryBufferThreshold = Int32.MaxValue;
                });
                services.AddMvc(options =>
                {
                    options.MaxModelBindingCollectionSize = int.MaxValue;
                });
    
    

    Web.Config

    <system.web>
        <httpRuntime  maxRequestLength="1048576" />  
    </system.web>
        <system.webServer>
      <security>
        <requestFiltering>
          <requestLimits maxAllowedContentLength="2147483648" />
        </requestFiltering>
      </security>
    </system.webServer>
    <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="50000000"/>
               </webServices>
           </scripting>
       </system.web.extensions>
    
    

    【讨论】:

    • 我尝试在端点方法上添加装饰器,[RequestFormLimits(MultipartBodyLengthLimit = 104857600)],但没有任何改变当大小大于 5~600 KB 时我仍然收到一个 emty 对象
    • @Giox 我看到你的参数是objVal。请告诉我它是绑定单曲还是合集?你很明显地使用这个 IFormFile 类型。因为它是文件过程上传。您可以为查找集合表单模型使用另一个参数。但是对于上传文件。您应该为文件使用额外的参数。您应该为上传文件使用 IFromFile 类型参数。希望现在您理解了。然后它就可以正常工作了
    • 我不是在上传文件,而是在发送一个 JSON 字符串,这可能会很长,具体取决于生成的内容。
    • @Giox 现在检查我的更新答案。我认为它应该可以工作。
    • 没什么,我已经尝试应用你的所有建议,但是当请求大于 500KB 时,参数 objVal 始终为空
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 2018-04-21
    • 1970-01-01
    • 2023-03-17
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多