我在Docker中使用ASP.NET Core 2.2。我需要为上传文件添加1 MB的限制,并在超出限制时返回400错误的请求。
我尝试在操作中添加[RequestSizeLimit(1000000)]
[HttpPost("{id:int}/configs")]
[RequestSizeLimit(1000000)]
public async Task<IActionResult> Add(int id, IFormFile file)
,然后在我的ErrorHandlingMiddleware中:
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception ex)
{
switch (ex)
{
...
case BadHttpRequestException badRequestException when badRequestException.Message == "Request body too large.":
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
await context.WriteResponse(new ErrorResponse("request", badRequestException.Message));
break;
}
}
当我在没有docker的调试中运行它时,它返回400错误请求。但是如果它在docker中运行,那么我得到了: 状态码413请求实体太大且内容
<html>
<head>
<title>413 Request Entity Too Large</title>
</head>
<body>
<center>
<h1>413 Request Entity Too Large</h1>
</center>
<hr>
<center>nginx/1.15.6</center>
</body>
</html>
如何设置限制并返回400 Bab请求?
我关注这篇文章:http://www.binaryintellect.net/articles/612cf2d1-5b3d-40eb-a5ff-924005955a62.aspx,但我需要返回400并且没有web.config