如何在 netcore 3 中多次读取 http 请求正文?

编程入门 行业动态 更新时间:2024-10-25 23:26:04
本文介绍了如何在 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; } }

但是,当此请求传递给控制器​​时,请求正文为空.

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();

我的理解是现在替换为

httpContext.Request.EnableBuffering();

然而,即使有

httpContext.Request.EnableBuffering();

一旦请求正文被读取,请求正文仍然为空.

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

我该如何解决这个问题?

How can I get around this ?

推荐答案

这是一个已知的问题 在 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 请求正文?

本文发布于:2023-10-09 18:45:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476477.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   正文   netcore   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!