无法访问ASP.NET Core 3.1异常处理程序中的HTTP请求正文内容 [英] Unable to access HTTP request body content in ASP.NET Core 3.1 exception handler

查看:73
本文介绍了无法访问ASP.NET Core 3.1异常处理程序中的HTTP请求正文内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET Core 3.1似乎使得异常后访问原始HTTP请求正文非常困难

It seems .NET Core 3.1 makes it very difficult to access the raw HTTP request body following an exception

问题的核心似乎是您无法将流倒带到开头,因此整个内容都可以阅读

The core of the issue seems to be you can't rewind the stream to the beginning so the whole thing can be read

在下面的示例中,输出为:

In the below sample the output is:

Content-Length: 19
Can't rewind body stream. Specified method is not supported.
Body:

如果我删除了Seek,也没有例外,但是主体自然会以空字符串形式返回,因为它已经被处理了

If I remove the Seek, there's no exception, but of course the body comes back as an empty string, because it's already been processed

围绕Pipeline API的一些新管道涉及Request.BodyReader,但它们也遇到相同的问题.

There is some new plumbing based around the Pipeline API involving Request.BodyReader but they suffer from the same problem.

在发生异常之后,我要做的就是将完整请求转储到包含正文内容的日志文件中.当然这应该没有那么困难吗?

All I need to do, following an exception, is dump the full request to a log file including the body content. Surely this should not be so difficult?

   public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {            
        app.UseExceptionHandler(errorApp =>
        {
            errorApp.Run(async context =>
            {
                Console.WriteLine("Content-Length: " + context.Request.Headers["Content-Length"]);
                string tmp;
                try
                {
                    context.Request.Body.Seek(0, SeekOrigin.Begin);
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Can't rewind body stream. " + ex.Message);
                }
                using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                {

                    tmp = await reader.ReadToEndAsync();
                }

                Console.WriteLine("Body: " + tmp);

            });
        });

        app.UseStaticFiles();
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
            endpoints.MapDefaultControllerRoute();
        });
    }

推荐答案

请通过在代码中添加以下内容:

Add the following to your code:

Startup.cs

app.Use((context, next) =>
{
    context.Request.EnableBuffering(); // calls EnableRewind() `https://github.com/dotnet/aspnetcore/blob/4ef204e13b88c0734e0e94a1cc4c0ef05f40849e/src/Http/Http/src/Extensions/HttpRequestRewindExtensions.cs#L23`
    return next();
});

然后,您应该可以按照上面的代码进行倒带:

You should then be able to rewind as per your code above:

string tmp;
try
{
    context.Request.Body.Seek(0, SeekOrigin.Begin);
}
catch(Exception ex)
{
    Console.WriteLine("Can't rewind body stream. " + ex.Message);
}
using (var reader = new StreamReader(context.Request.Body, Encoding.UTF8))
{

    tmp = await reader.ReadToEndAsync();
}

这篇关于无法访问ASP.NET Core 3.1异常处理程序中的HTTP请求正文内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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