15

I'm trying to upload multiple files in my .NET Core v3.1 Blazor application, but I can't get passed the 30MB limit.
Searching for this I found Increase upload file size in Asp.Net core and tried the suggestions but it doesn't work.
All found solutions involve changing web.config, but I don't have that file.
Also my application runs in Visual Studio 2019 during development but will also run on Azure as a WebApp.

These are my settings:
program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>().ConfigureKestrel((context, options) =>
            {
                options.Limits.MaxRequestBodySize = null;
            });
        });

UploadController.cs

[Authorize]
[DisableRequestSizeLimit]
public class UploadController : BaseApiController

ConfigureServices in Startup.cs

services.AddSignalR(e => e.MaximumReceiveMessageSize = 102400000)
    .AddAzureSignalR(Configuration["Azure:SignalR:ConnectionString"]);

services.Configure<FormOptions>(options =>
{
    options.ValueLengthLimit = int.MaxValue;
    options.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
    options.MultipartBoundaryLengthLimit = int.MaxValue;
    options.MultipartHeadersCountLimit = int.MaxValue;
    options.MultipartHeadersLengthLimit = int.MaxValue;
});
services.Configure<IISServerOptions>(options =>
{
    options.MaxRequestBodySize = int.MaxValue;
});

Configure in Startup.cs

app.Use(async (context, next) =>
{
    context.Features.Get<IHttpMaxRequestBodySizeFeature>()
        .MaxRequestBodySize = null;

    await next.Invoke();
});

Did I miss a setting? Can't believe this needs to be so hard.

Paul Meems
  • 3,002
  • 4
  • 35
  • 66

2 Answers2

1

Decorate your controller method with the [DisableRequestSizeLimit] attribute.

[HttpPost]
[DisableRequestSizeLimit]
[Route("~/upload")]
public async Task<IActionResult> Upload(...)
{
    return Ok(...);
}
hunter
  • 62,308
  • 19
  • 113
  • 113
0

Found the solution at https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-3.1 . Minimized solution is only a newly added web.config file with the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

There seems to be some other settings, such as limiting per action method. You may need to go over them and pick whatever best suits your needs.

p.s. Saw the same web.config solution at somewhere else earlier today. Tried 30M as maxAllowedContentLength, and it did not work for a ~10MB text file. Now realized request size gets tripled because file content is sent as a binary array's string representation (which is a problem and should be handled). Check Network tab for the exact request size and be sure it is not exceeding the above web.config setting.

burkay
  • 1,075
  • 1
  • 10
  • 20