猿问

HTTP 500 而不是 ASP.NET Core 2.1.401 中的 413

我有[RequestSizeLimit]我的 API 控制器,它有点像预期的那样工作:大于指定限制的请求被拒绝。


    [HttpPut]

    [RequestSizeLimit(120_000_000)]

    public async Task<IActionResult> Put(IFormCollection form)

    {

       ...

    }

问题是,抛出异常:


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.TryInit()

   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.MessageBody.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)

   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)

因此返回 HTTP 500,但我希望返回 413 或 400。而且我不希望出现异常,因为这是完全正常的情况。


找不到有关此的任何文档。对于太大的请求,返回 413 的正确方法是什么?


米琪卡哇伊
浏览 188回答 3
3回答

侃侃尔雅

Kestrel 响应 413 Payload Too Large 响应,但 HttpSys 响应通用 500 Internal Server Error 响应。我假设您使用第二个。在这种情况下,您可以实现异常处理 middelware 来处理这种情况:public class ExceptionMiddleware{&nbsp; &nbsp; private readonly RequestDelegate _next;&nbsp; &nbsp; public ExceptionMiddleware(RequestDelegate next)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _next = next;&nbsp; &nbsp; }&nbsp; &nbsp; public async Task InvokeAsync(HttpContext httpContext)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await _next(httpContext);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HandleExceptionAsync(httpContext, ex);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static void HandleExceptionAsync(HttpContext context, Exception exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (exception is BadHttpRequestException badRequestException && badRequestException.Message == "Request body too large.")&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; context.Response.StatusCode = (int) HttpStatusCode.RequestEntityTooLarge;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}并在 Startup.cs 中的 Configure 中注册:public void Configure(IApplicationBuilder app){&nbsp; &nbsp; ...&nbsp; &nbsp; app.UseMiddleware<ExceptionMiddleware>();&nbsp; &nbsp; ...}

慕莱坞森

基于 Alex Riabov 的回答,现在可以在不解析文本的情况下捕获实际的内部错误代码。所以异常处理程序的更新部分是:private static void HandleExceptionAsync(HttpContext context, Exception exception){&nbsp; &nbsp; if (exception is BadHttpRequestException badRequestException && badRequestException.StatusCode == StatusCodes.Status413PayloadTooLarge)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; context.Response.StatusCode = badRequestException.StatusCode;&nbsp; &nbsp; }}

海绵宝宝撒

我在 Visual Studio 的调试中运行时遇到了同样的行为。我发现了它的DeveloperExceptionPageMiddleware原因。我fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware在BadHttpRequestException异常上方的服务器日志中注意到。Startup.cs如预期的那样,从我的中删除它会给出 413 个响应。if (env.IsDevelopment())&nbsp; &nbsp; app.UseDeveloperExceptionPage();现在显示服务器日志fail: Microsoft.AspNetCore.Server.Kestrel。
随时随地看视频慕课网APP
我要回答