请求未被 413 状态码拒绝
Request not being denied with 413 status code
需要拒绝具有任何正文内容的请求(意味着 body
大小 > 0)。我尝试使用 RequestSizeLimit
属性,但它似乎无法正常工作。
代码:
[HttpPost]
[RequestSizeLimit(0)]
public IActionResult Test()
{
return Ok();
}
我正在使用 Postman
进行测试。提供 "qwerty" 作为 POST
请求正文的值。这是 Kestrel
日志的样子:
info: Microsoft.AspNetCore.Server.Kestrel[17]
Connection id "0HLN06I1687S4" bad request data: "Request body too large."
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException:
Request body too large. at
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason
reason) at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.ForContentLength.OnReadStarting()
at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryStart()
at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ConsumeAsync()
at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication 1
application) at
Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication 1
application)
尽管如此 - 我仍然看到 200 (OK) 响应。我可以毫无问题地调试该方法。看起来 - 过滤器工作正常 - 但由于某种原因它没有触发异常。预期的行为 - 是 "payload too large" (413) 为请求返回的状态,并且未触发方法中的代码执行。
任何想法或解释 - 为什么我会看到这种行为?
这不是问题的答案,而是我的问题的解决方案。我自己编写了一个按预期工作的操作过滤器的实现。
public class PayloadMaximumSizeFilter : ActionFilterAttribute
{
private long _maxContentLength;
private string _message;
public PayloadMaximumSizeFilter(long maxContentLength)
{
this._maxContentLength = maxContentLength;
}
public PayloadMaximumSizeFilter(long maxContentLength, string message)
{
this._maxContentLength = maxContentLength;
this._message = message;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
long? contentLength = filterContext.HttpContext.Request.ContentLength;
if (contentLength.HasValue && contentLength.Value > _maxContentLength)
{
filterContext.Result = new JsonResult(filterContext.ModelState)
{
Value = _message ?? "Request body too large.",
StatusCode = 413
};
}
}
}
需要拒绝具有任何正文内容的请求(意味着 body
大小 > 0)。我尝试使用 RequestSizeLimit
属性,但它似乎无法正常工作。
代码:
[HttpPost]
[RequestSizeLimit(0)]
public IActionResult Test()
{
return Ok();
}
我正在使用 Postman
进行测试。提供 "qwerty" 作为 POST
请求正文的值。这是 Kestrel
日志的样子:
info: Microsoft.AspNetCore.Server.Kestrel[17] Connection id "0HLN06I1687S4" bad request data: "Request body too large." Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large. at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.ForContentLength.OnReadStarting() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.TryStart() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ConsumeAsync() at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication 1 application) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequestsAsync[TContext](IHttpApplication 1 application)
尽管如此 - 我仍然看到 200 (OK) 响应。我可以毫无问题地调试该方法。看起来 - 过滤器工作正常 - 但由于某种原因它没有触发异常。预期的行为 - 是 "payload too large" (413) 为请求返回的状态,并且未触发方法中的代码执行。
任何想法或解释 - 为什么我会看到这种行为?
这不是问题的答案,而是我的问题的解决方案。我自己编写了一个按预期工作的操作过滤器的实现。
public class PayloadMaximumSizeFilter : ActionFilterAttribute
{
private long _maxContentLength;
private string _message;
public PayloadMaximumSizeFilter(long maxContentLength)
{
this._maxContentLength = maxContentLength;
}
public PayloadMaximumSizeFilter(long maxContentLength, string message)
{
this._maxContentLength = maxContentLength;
this._message = message;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
long? contentLength = filterContext.HttpContext.Request.ContentLength;
if (contentLength.HasValue && contentLength.Value > _maxContentLength)
{
filterContext.Result = new JsonResult(filterContext.ModelState)
{
Value = _message ?? "Request body too large.",
StatusCode = 413
};
}
}
}