By default, ASP.NET Core allows you to upload files up of (approximately) 28 MB in size. However, for an application I had to update this limit to support files up to 128MB.
Letās see what should be done to get this working in ASP.NET Core:
Open your Startup.cs and write the following code in the ConfigureServices():
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddControllers(); | |
services.Configure<FormOptions>(options => | |
{ | |
// Set the limit to 128 MB | |
options.MultipartBodyLengthLimit = 134217728; | |
}); | |
} |
The code above configures the FormOptions and sets the MultipartBodyLengthLimit property to 128MB.
Unfortunately we are not done yet. Depending on if you use Kestrel or host your application in IIS (Express), some extra work is required.
IIS (Express)
For IIS (Express) you need to update your web.config and add the following configuration section:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<system.webServer> | |
<!-- Removed other configuration --> | |
<security> | |
<requestFiltering> | |
<!-- 128MB = 134,217,728 Bytes --> | |
<requestLimits maxAllowedContentLength="134217728"/> | |
</requestFiltering> | |
</security> | |
</system.webServer> | |
</configuration> |
Kestrel
For Kestrel, you need to add some extra code in your Program.cs file:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static IWebHostBuilder CreateWebHostBuilder | |
(string[] args) => | |
WebHost.CreateDefaultBuilder(args) | |
.UseStartup<Startup>() | |
.UseKestrel(options => | |
{ | |
options.Limits.MaxRequestBodySize = 134217728; | |
}); |