5

I am using ASP.net Core 2.0 with MVC. I have a controller action that I want to limit the request size to 1MB. I added the RequestSizeLimit attribute like so:

[HttpPost]
[Authorize]
[RequestSizeLimit(1_000_000)]
public async Task<List<ResourceUploadResult>> Upload([FromBody]List<Resource> updatedList){
    //....
}

When the upload is < 1MB, it works as expected. When it is > 1MB I expected the server to return a status of 413, but instead, the updatedList parameter is null and the action executes normally, running into a NullReferenceException when it tries to iterate the list.

Is there a way to tell Kestrel to return 413 when the size limit is reached?

spectacularbob
  • 3,080
  • 2
  • 20
  • 41
  • I test your code with `.NET Core 2.0.0`, but it runs flawlessly. Is there a project that can reproduce this issue ? – itminus Dec 21 '18 at 04:52

3 Answers3

1

Probably not the best, but it will work in the mean time.

if(updatedList == null)
      return StatusCode(413, "Payload to big") ;
Mikes3ds
  • 592
  • 5
  • 12
1

you can limit the size globally by

.UseKestrel(kestrolOptions =>
{
    kestrolOptions.Limits.MaxRequestBodySize = 1_000_000;
..
0

The problem went away when I upgraded to 2.1. I can't say for sure that 2.0 has a bug, but after updating all the NuGet packages to 2.1 it behaved as expected.

It's unfortunate that I don't have more information about the cause since others seeking a solution to this problem may not be able to just upgrade their version like I did.

spectacularbob
  • 3,080
  • 2
  • 20
  • 41