gpt4 book ai didi

c# - 如果我在启动文件中添加中间件。那个时候在模型中没有得到请求

转载 作者:行者123 更新时间:2023-11-30 22:51:25 28 4
gpt4 key购买 nike

这个中间件在启动文件中添加。

app.UseMiddleware<RequestResponseLoggingMiddleware>();

中间件是

public class RequestResponseLoggingMiddleware
{
private readonly RequestDelegate _next;

public RequestResponseLoggingMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task Invoke(HttpContext context)
{
//First, get the incoming request
var request = await FormatRequest(context.Request);

//Copy a pointer to the original response body stream
var originalBodyStream = context.Response.Body;

//Create a new memory stream...
using (var responseBody = new MemoryStream())
{
//...and use that for the temporary response body
context.Response.Body = responseBody;

//Continue down the Middleware pipeline, eventually returning to this class
await _next(context);

//Format the response from the server
var response = await FormatResponse(context.Response);

//TODO: Save log to chosen datastore

//Copy the contents of the new memory stream (which contains the response) to the original stream, which is then returned to the client.
await responseBody.CopyToAsync(originalBodyStream);
}
}

private async Task<string> FormatRequest(HttpRequest request)
{
var body = request.Body;

//This line allows us to set the reader for the request back at the beginning of its stream.
request.EnableRewind();

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

//..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 async Task<string> FormatResponse(HttpResponse response)
{
//We need to read the response stream from the beginning...
response.Body.Seek(0, SeekOrigin.Begin);

//...and copy it into a string
string text = await new StreamReader(response.Body).ReadToEndAsync();

//We need to reset the reader for the response so that the client can read it.
response.Body.Seek(0, SeekOrigin.Begin);

//Return the string for the response, including the status code (e.g. 200, 404, 401, etc.)
return $"{response.StatusCode}: {text}";
}
}

使用此中间件获取请求,但随后不在原始模型类中传递该请求。

这个中间件添加在启动文件中。当时就面临这个问题。

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

最佳答案

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

1.交换var body = request.Body;request.EnableRewind();的顺序

2.在request.Body = body;之前插入body.Seek(0, SeekOrigin.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}";
}

关于c# - 如果我在启动文件中添加中间件。那个时候在模型中没有得到请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59193071/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com