Asp.net Core RequestSizeLimit still executes action

醉酒当歌 提交于 2020-05-15 02:20:26

问题


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?


回答1:


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

if(updatedList == null)
      return StatusCode(413, "Payload to big") ;



回答2:


you can limit the size globally by

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



回答3:


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.



来源:https://stackoverflow.com/questions/53877676/asp-net-core-requestsizelimit-still-executes-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!