Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果我在启动文件中添加中间件。该时间未在模型中获取请求_C#_.net_Asp.net Core_Middleware - Fatal编程技术网

C# 如果我在启动文件中添加中间件。该时间未在模型中获取请求

C# 如果我在启动文件中添加中间件。该时间未在模型中获取请求,c#,.net,asp.net-core,middleware,C#,.net,Asp.net Core,Middleware,此中间件添加到启动文件中 app.UseMiddleware<RequestResponseLoggingMiddleware>(); app.UseMiddleware(); 中间件是 公共类RequestResponseLoggingMiddleware { private readonly RequestDelegate\u next; 公共RequestResponseLoggingMiddleware(RequestDelegate下一步) { _下一个=下一个; }

中间件添加到启动文件中

app.UseMiddleware<RequestResponseLoggingMiddleware>();
app.UseMiddleware();
中间件是

公共类RequestResponseLoggingMiddleware
{
private readonly RequestDelegate\u next;
公共RequestResponseLoggingMiddleware(RequestDelegate下一步)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext上下文)
{
//首先,获取传入的请求
var request=wait FormatRequest(context.request);
//复制指向原始响应体流的指针
var originalBodyStream=context.Response.Body;
//创建一个新的内存流。。。
使用(var responseBody=newmemorystream())
{
//…并将其用于临时响应机构
context.Response.Body=responseBody;
//继续中间件管道,最终返回到这个类
等待下一步(上下文);
//格式化来自服务器的响应
var response=wait FormatResponse(context.response);
//TODO:将日志保存到所选数据存储
//将新内存流(包含响应)的内容复制到原始流,然后将原始流返回给客户端。
wait responseBody.CopyToAsync(originalBodyStream);
}
}
专用异步任务FormatRequest(HttpRequest请求)
{
var body=request.body;
//这一行允许我们将请求的读取器设置回其流的开头。
request.EnableRewind();
//我们现在需要读取请求流。首先,我们创建一个与请求流长度相同的新字节[]。。。
var buffer=新字节[Convert.ToInt32(request.ContentLength)];
//…然后我们将整个请求流复制到新的缓冲区中。
wait request.Body.ReadAsync(buffer,0,buffer.Length);
//我们使用UTF8编码将字节[]转换为字符串。。。
var bodyAsText=Encoding.UTF8.GetString(缓冲区);
//..最后,将读取主体分配回请求主体,这是允许的,因为EnableRewind()
请求。Body=Body;
返回$“{request.Scheme}{request.Host}{request.Path}{request.QueryString}{bodyAsText}”;
}
专用异步任务FormatResponse(HttpResponse响应)
{
//我们需要从一开始就读取响应流。。。
response.Body.Seek(0,SeekOrigin.Begin);
//…并将其复制到字符串中
string text=等待新的StreamReader(response.Body).ReadToEndAsync();
//我们需要为响应重置读取器,以便客户端可以读取响应。
response.Body.Seek(0,SeekOrigin.Begin);
//返回响应的字符串,包括状态代码(例如200、404、401等)
返回$“{response.StatusCode}:{text}”;
}
}
将此中间值用于get请求,但之后该请求将在原始模型类中传递

此中间件添加到启动文件中。当时面临着这个问题


因此,在传入模型类之前,任何其他获取请求的方法。我还需要得到无效的请求。。因为我需要将其与日志一样插入数据库。

您的
格式请求中存在一些问题,请尝试应用以下更改:

1.交换
var body=request.body的顺序
请求。启用倒带()

2.在
request.Body=Body之前插入
body.Seek(0,请参见原代码.Begin)

最终代码:

private async Task<string> FormatRequest(HttpRequest request)
    {
        //This line allows us to set the reader for the request back at the beginning of its stream.
        request.EnableRewind();

        var body = request.Body;

        //We now need to read the request stream.  First, we create a new byte[] with the same length as the request stream...
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];

        //...Then we copy the entire request stream into the new buffer.
        await request.Body.ReadAsync(buffer, 0, buffer.Length);

        //We convert the byte[] into a string using UTF8 encoding...
        var bodyAsText = Encoding.UTF8.GetString(buffer);

        body.Seek(0, SeekOrigin.Begin);
        //..and finally, assign the read body back to the request body, which is allowed because of EnableRewind()
        request.Body = body;

        return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}";
    }
private异步任务格式化请求(HttpRequest请求)
{
//这一行允许我们将请求的读取器设置回其流的开头。
request.EnableRewind();
var body=request.body;
//我们现在需要读取请求流。首先,我们创建一个与请求流长度相同的新字节[]。。。
var buffer=新字节[Convert.ToInt32(request.ContentLength)];
//…然后我们将整个请求流复制到新的缓冲区中。
wait request.Body.ReadAsync(buffer,0,buffer.Length);
//我们使用UTF8编码将字节[]转换为字符串。。。
var bodyAsText=Encoding.UTF8.GetString(缓冲区);
body.Seek(0,SeekOrigin.Begin);
//..最后,将读取主体分配回请求主体,这是允许的,因为EnableRewind()
请求。Body=Body;
返回$“{request.Scheme}{request.Host}{request.Path}{request.QueryString}{bodyAsText}”;
}

您的
格式请求中存在一些问题,请尝试应用以下更改:

1.交换
var body=request.body的顺序
请求。启用倒带()

2.在
request.Body=Body之前插入
body.Seek(0,请参见原代码.Begin)

最终代码:

private async Task<string> FormatRequest(HttpRequest request)
    {
        //This line allows us to set the reader for the request back at the beginning of its stream.
        request.EnableRewind();

        var body = request.Body;

        //We now need to read the request stream.  First, we create a new byte[] with the same length as the request stream...
        var buffer = new byte[Convert.ToInt32(request.ContentLength)];

        //...Then we copy the entire request stream into the new buffer.
        await request.Body.ReadAsync(buffer, 0, buffer.Length);

        //We convert the byte[] into a string using UTF8 encoding...
        var bodyAsText = Encoding.UTF8.GetString(buffer);

        body.Seek(0, SeekOrigin.Begin);
        //..and finally, assign the read body back to the request body, which is allowed because of EnableRewind()
        request.Body = body;

        return $"{request.Scheme} {request.Host}{request.Path} {request.QueryString} {bodyAsText}";
    }
private异步任务格式化请求(HttpRequest请求)
{
//这一行允许我们将请求的读取器设置回其流的开头。
request.EnableRewind();
var body=request.body;
//我们现在需要读取请求流。首先,我们创建一个与请求流长度相同的新字节[]。。。
var buffer=新字节[Convert.ToInt32(request.ContentLength)];
//…然后我们将整个请求流复制到新的缓冲区中。
wait request.Body.ReadAsync(buffer,0,buffer.Length);
//我们使用UTF8编码将字节[]转换为字符串。。。
var bodyAsText=Encoding.UTF8.GetString(缓冲区);
body.Seek(0,SeekOrigin.Begin);
//..最后,将读取主体分配回请求主体,这是允许的,因为EnableRewind()
请求。Body=Body;
返回$“{request.Scheme}{request.Host}{request.Path}{request.QueryString}{bodyAsText}”;
}

您能分享一下star的完整配置方法吗