Asp.net Core RequestSizeLimit仍执行操作 [英] Asp.net Core RequestSizeLimit still executes action

查看:206
本文介绍了Asp.net Core RequestSizeLimit仍执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将ASP.net Core 2.0与MVC一起使用.我有一个控制器动作,我想将请求大小限制为1MB.我像这样添加了 RequestSizeLimit 属性:

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){
    //....
}

当上传为<1MB,它可以正常工作.当它大于1MB时,我希望服务器返回状态413,但是 updatedList 参数为null,该操作正常执行,并在运行时运行到 NullReferenceException 尝试对列表进行迭代.

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.

是否有一种方法可以告诉Kestrel在达到大小限制时返回413?

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

推荐答案

您可以通过以下方式全局限制大小:

you can limit the size globally by

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

这篇关于Asp.net Core RequestSizeLimit仍执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆