HttpContext.Response.Body.Position = 0-“不支持指定的方法"错误 [英] HttpContext.Response.Body.Position = 0 - "Specified method is not supported" error

查看:108
本文介绍了HttpContext.Response.Body.Position = 0-“不支持指定的方法"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了一些日志记录中间件,该中间件可以使用HttpContext捕获和记录信息.

I've got some logging middleware I've setup that grabs and logs information utilizing HttpContext.

我需要将HttpResponse.Body的位置设置为0才能读取整个流,但是,无论我如何尝试,它都会抛出不支持指定的方法"并失败.

I need to set the position of the HttpResponse.Body to 0 in order to read the whole stream, however, no matter what I try, it throws "Specified method is not supported" and fails.

这对我来说很奇怪,因为HttpResponse.Body中已经内置了位置,并且在成功之前我已经使用过它.

This is very weird to me because position is built right into HttpResponse.Body and I've used it before successfully.

我还尝试过将HttpResponse.Body.Seek与相同的结果一起使用.

I also tried to use HttpResponse.Body.Seek with the same result.

在这一点上,我很受困扰,我们将不胜感激.

At this point I'm stuck, any help would be appreciated.

更新:一旦将response.body位置移动到新的内存流中,我就能够更改它的位置,但是现在它返回了一个空的正文.

UPDATE: I was able to get the response.body position to change once I moved it into a new memory stream, however, now it returns an empty body back.

public async Task Invoke(HttpContext context)
        {
            //Retrieve request & response
            var request = context.Request;
            var response = context.Response;

            if (request.Path != "/")
            {
                var reqBody = request.Body;
                var resBody = response.Body;
                string path = request.Path;
                string method = request.Method;
                string queryString = HttpUtility.UrlDecode(request.QueryString.ToString());
                int statusCode = context.Response.StatusCode;

                var buffer = new byte[Convert.ToInt32(request.ContentLength)];
                await request.Body.ReadAsync(buffer, 0, buffer.Length);
                var reqBodyText = Encoding.UTF8.GetString(buffer);
                request.Body = reqBody;

                var responseBodyStream = new MemoryStream();
                context.Response.Body = responseBodyStream;

                await _next(context);

                responseBodyStream.Seek(0, SeekOrigin.Begin);
                var resBodyText = new StreamReader(responseBodyStream).ReadToEnd();
                responseBodyStream.Seek(0, SeekOrigin.Begin);
                await responseBodyStream.CopyToAsync(context.Response.Body);

                ...
            }
        }

推荐答案

我能够解决此问题:

首先,我将响应设置为它自己的内存流,并在设置了流后调用await _next(context):

Firstly, I set the response to its own memory stream and call await _next(context) after the stream was set:

var responseBodyStream = new MemoryStream();
context.Response.Body = responseBodyStream;

await _next(context);

然后,一旦执行此操作,我发现我得到了一个空的主体,这是由于尝试将一个空的主体设置为响应上下文而造成的:

Then once I did this, I noticed I was getting an empty body back, this was due to trying to set an empty body back as the response context:

await responseBodyStream.CopyToAsync(context.Response.Body);

我删除了这一行,一切都开始正常工作.

I removed this line and everything started working correctly.

这篇关于HttpContext.Response.Body.Position = 0-“不支持指定的方法"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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