如何在ASP Net Core 2.2中间件中多次读取请求正文?

时间:2019-01-30 14:12:57

标签: c# middleware asp.net-core-2.2

我尝试了这个: Read request body twice 还有这个: https://github.com/aspnet/Mvc/issues/4962 但没有用。 我这样阅读请求正文:

app.Use(async (context, next) =>
{
    var requestBody = await ReadStream(context.Request.Body);
    var requestPath = context.Request.Path.ToString();
    //Do some thing

    await next.Invoke();

    var responseStatusCode = context.Response.StatusCode;
    //Do some other thing
});

private async Task<string> ReadStream(Stream stream)
{
    using (var streamReader = new StreamReader(stream))
    {
        var result = await streamReader.ReadToEndAsync();

        return result;
    }
}

在控制器中,我得到了“处置对象”或“空流”。

5 个答案:

答案 0 :(得分:8)

上面@HoussamNasser回答的

.netcore 3.1版本。我创建了一个可重用的函数来读取请求正文。请注意更改:HttpRequestRewindExtensions.EnableBuffering(request)。现在,EnableBuffering已成为HttpRequestRewindExtensions类的一部分。

public async Task<JObject> GetRequestBodyAsync(HttpRequest request)
    {
        JObject objRequestBody = new JObject();

        // IMPORTANT: Ensure the requestBody can be read multiple times.
        HttpRequestRewindExtensions.EnableBuffering(request);

        // IMPORTANT: Leave the body open so the next middleware can read it.
        using (StreamReader reader = new StreamReader(
            request.Body,
            Encoding.UTF8,
            detectEncodingFromByteOrderMarks: false,
            leaveOpen: true))
        {
            string strRequestBody = await reader.ReadToEndAsync();
            objRequestBody = SerializerExtensions.Deserialize<JObject>(strRequestBody);

            // IMPORTANT: Reset the request body stream position so the next middleware can read it
            request.Body.Position = 0;
        }

        return objRequestBody;
    }

此函数将返回一个JObject,该JObject可用于读取请求主体对象的属性。 SerializerExtensions是我用于序列化和反序列化的自定义扩展。

在中间件中,您可以在构造函数中注入IHttpContextAccessor httpContextAccessor。然后访问HttpRequest request = _httpContextAccessor.HttpContext.Request;之类的Request对象。最后,可以调用GetRequestBodyAsync(request)

之类的可重用函数

答案 1 :(得分:1)

经过更多的挣扎和使用 “ context.Request.EnableRewind()” 终于像这样工作了:

app.Use(async (context, next) =>
{
    context.Request.EnableRewind();
    var stream = context.Request.Body;

    using (var reader = new StreamReader(stream))
    {
        var requestBodyAsString = await reader.ReadToEndAsync();

        if (stream.CanSeek)
            stream.Seek(0, SeekOrigin.Begin);

        //Do some thing

        await next.Invoke();

        var responseStatusCode = context.Response.StatusCode;
        //Do some other thing
    }
});

答案 2 :(得分:0)

这可能是因为using各地的语句您的StreamReader。 using将StreamReader的调用置于基础流上。详情参见答案here。您可以尝试保留参考并在StreamReader完成后处置await next.Invoke();

答案 3 :(得分:0)

第二次读取流时,流指针将设置到最后一个位置。您应该尝试将其移回零位,以便从头开始再次阅读。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;

namespace Test_Middlewares.Middlewares
{
    // You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
    public class LoggingMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly ILogger<LoggingMiddleware> _logger;

        public LoggingMiddleware(RequestDelegate next, ILogger<LoggingMiddleware> logger)
        {
            _next = next;
            _logger = logger;
        }

        public async Task InvokeAsync(HttpContext httpContext)
        {
            var HttpContextBody = httpContext.Request.Body;
            string requestBody = "";

            httpContext.Request.EnableBuffering();

            // Leave the body open so the next middleware can read it.
            using (var reader = new StreamReader(
                httpContext.Request.Body,
                encoding: Encoding.UTF8,
                detectEncodingFromByteOrderMarks: false,
                bufferSize: -1,
                leaveOpen: true))
            {
                var body = await reader.ReadToEndAsync();
                // Do some processing with body…

                // Reset the request body stream position so the next middleware can read it
                httpContext.Request.Body.Position = 0;
            }

            _logger.LogDebug("Middleware 1 body =" + requestBody);

            await _next.Invoke(httpContext);
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class LoggingMiddlewareExtensions
    {
        public static IApplicationBuilder UseLoggingMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<LoggingMiddleware>();
        }
    }
}

有关更多信息,请参考以下链接:

https://devblogs.microsoft.com/aspnet/re-reading-asp-net-core-request-bodies-with-enablebuffering/

https://gunnarpeipman.com/aspnet-core-request-body/

答案 4 :(得分:0)

我一直在挣扎,.net core 3.1发生了重大变化,“ context.Request.EnableRewind();” .net core 3.1中没有更多可用的内容,更有用的技巧visit here

asp.net mvc api .net core 3.1(startup.cs)中的工作代码

            app.Use(async (context, next) =>
        {
            context.Request.EnableBuffering();
            var stream = context.Request.Body;

            using (var reader = new StreamReader(stream))
            {
                var requestBodyAsString = await reader.ReadToEndAsync();

                if (stream.CanSeek)
                    stream.Seek(0, SeekOrigin.Begin);
               
                //some logging and other stuff goes here

                await next.Invoke();

            }
        });