如何多次读取netcore 3中的http请求正文? [英] How can I read http request body in netcore 3 more than once?

查看:113
本文介绍了如何多次读取netcore 3中的http请求正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个netcore 3 API应用程序,该应用程序记录传入的请求,然后将其传递给控制器​​操作.

I have a netcore 3 API application that logs the incoming request and then passes it on to the controller action.

我的代码如下:

public RequestLoggingHandler(IHttpContextAccessor httpContextAccessor)
{
    _httpContextAccessor = httpContextAccessor;
}


protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RequestLoggingRequirement requirement)
{
    try
    {
        var httpContext = _httpContextAccessor.HttpContext;
        var request = httpContext.Request;

        _repository = (ICleanupOrderRepository)httpContext.RequestServices.GetService(typeof(ICleanupOrderRepository));
        _cache = (IMemoryCache)httpContext.RequestServices.GetService(typeof(IMemoryCache));

        httpContext.Items["requestId"] = SaveRequest(request);

        context.Succeed(requirement);

        return Task.CompletedTask;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

private int SaveRequest(HttpRequest request)
{
    try
    {
        // Allows using several time the stream in ASP.Net Core
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];
       request.Body.ReadAsync(buffer, 0, buffer.Length);

        var requestContent = Encoding.UTF8.GetString(buffer);
        var requestId = _repository.SaveRawHandlerRequest($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {requestContent}");

        return requestId;
    }
    catch (Exception ex)
    {

        throw ex;
    }
}

但是,当此请求传递到Controller时,请求正文为null.

However when this request is passed on to the Controller the request body is null.

以前您可以在Core2.x中完成

Previously in Core2.x you could do

request.EnableRewind();

我的理解是,现在已替换为

My understanding is this is now replaced with

httpContext.Request.EnableBuffering();

但是,即使使用

httpContext.Request.EnableBuffering();

读取请求正文后,请求正文仍为null.

the request body is still null once the request body is read.

我该如何解决?

推荐答案

这是已知的问题在github上.

It is a known issue on github.

临时的解决方法是在调用EnableBuffering之后立即拉出主体,然后将流倒回至0而不进行处理:

A temp workaround is to pull out the body right after the call to EnableBuffering and then rewinding the stream to 0 and not disposing it:

public class RequestLoggingHandler : AuthorizationHandler<RequestLoggingRequirement>
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    public RequestLoggingHandler(IHttpContextAccessor httpContextAccessor)
    {
        _httpContextAccessor = httpContextAccessor;
    }
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, RequestLoggingRequirement requirement)
    {
        try
        {
            var httpContext = _httpContextAccessor.HttpContext;
            var request = httpContext.Request;
            request.EnableBuffering();

            httpContext.Items["requestId"] = SaveRequest(request);
            context.Succeed(requirement);

            return Task.CompletedTask;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private int SaveRequest(HttpRequest request)
    {
        try
        {
            // Allows using several time the stream in ASP.Net Core
            var buffer = new byte[Convert.ToInt32(request.ContentLength)];
            request.Body.ReadAsync(buffer, 0, buffer.Length);
            var requestContent = Encoding.UTF8.GetString(buffer);
            var requestId = _repository.SaveRawHandlerRequest($"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {requestContent}");

            request.Body.Position = 0;//rewinding the stream to 0
            return requestId;
        }
        catch (Exception ex)
        {

            throw ex;
        }
    }
}

这篇关于如何多次读取netcore 3中的http请求正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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