Boosting Upload File Size Limits in ASP.NET Core Applications
In ASP.NET Core 2.0 and later, both IIS and the Kestrel server impose limits on the size of uploaded files. This article details how to overcome these restrictions.
Method 1: Action-Specific Limit Adjustment
For fine-grained control, adjust the upload limit for individual MVC actions or controllers using the RequestSizeLimit
attribute. For example, to set a 100MB limit for the MyAction
method:
<code class="language-csharp">[HttpPost] [RequestSizeLimit(100_000_000)] public IActionResult MyAction([FromBody] MyViewModel data) { }</code>
To completely remove the limit for a specific action, use the [DisableRequestSizeLimit]
attribute.
Method 2: Middleware-Based Dynamic Configuration
For non-MVC applications, leverage the IHttpMaxRequestBodySizeFeature
to dynamically adjust the limit. Here's an example:
<code class="language-csharp">app.Run(async context => { context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000; });</code>
Method 3: Global Limit Modification
For a global solution, modify the MaxRequestBodySize
property within the UseKestrel
or UseHttpSys
configuration callbacks. Setting this property to null
disables the limit entirely.
<code class="language-csharp">.UseKestrel(options => { options.Limits.MaxRequestBodySize = null; });</code>
Choose the method that best suits your application's architecture and requirements. Remember to consider security implications when increasing upload limits.
The above is the detailed content of How Can I Increase Upload File Size Limits in ASP.NET Core?. For more information, please follow other related articles on the PHP Chinese website!