Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Core RequestSizeLimit still executes action

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?

like image 730
spectacularbob Avatar asked Oct 28 '22 23:10

spectacularbob


1 Answers

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

if(updatedList == null)
      return StatusCode(413, "Payload to big") ;
like image 178
Mikes3ds Avatar answered Nov 11 '22 16:11

Mikes3ds