RequestSizeLimitAttribute:HTTP 500而不是ASP.NET Core 2.1.401中的413 [英] RequestSizeLimitAttribute: HTTP 500 instead of 413 in ASP.NET Core 2.1.401

查看:299
本文介绍了RequestSizeLimitAttribute:HTTP 500而不是ASP.NET Core 2.1.401中的413的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API控制器上有 [RequestSizeLimit] ,它的 kinda 可以正常工作:大于指定限制的请求将被拒绝.

I have [RequestSizeLimit] on my API controller, and it kinda works as expected: requests bigger than specified limit are rejected.

    [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.而且我也不希望出现异常,因为这是完全正常的情况.

So HTTP 500 is returned, but I would expect 413 or 400. And I don't expect an exception, since this is a perfectly normal situation.

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

Could not find any documentation on this. What is the right way to return 413 for requests that are too big?

推荐答案

Kestrel响应为413 Payload Too Large,但HttpSys响应为通用500 Internal Server Error响应.我假设您使用第二个.在这种情况下,您可以实现异常处理中间件来处理这种情况:

Kestrel responds with a 413 Payload Too Large response, but HttpSys responds with a generic 500 Internal Server Error response. I assume you use the second one. In this case you can implement Exception handling middelware to handle this case:

public class ExceptionMiddleware
{
    private readonly RequestDelegate _next;

    public ExceptionMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext httpContext)
    {
        try
        {
            await _next(httpContext);
        }
        catch (Exception ex)
        {
            HandleExceptionAsync(httpContext, ex);
        }
    }

    private static void HandleExceptionAsync(HttpContext context, Exception exception)
    {
        if (exception is BadHttpRequestException badRequestException && badRequestException.Message == "Request body too large.")
        {
            context.Response.StatusCode = (int) HttpStatusCode.RequestEntityTooLarge;
        }
    }
}

并在Startup.cs中的"Configure"中注册它:

And register it in Configure in Startup.cs:

public void Configure(IApplicationBuilder app)
{
    ...
    app.UseMiddleware<ExceptionMiddleware>();
    ...
}

您也可以使用异常过滤器

这篇关于RequestSizeLimitAttribute:HTTP 500而不是ASP.NET Core 2.1.401中的413的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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