gpt4 book ai didi

c# - 有没有办法在.NET Core FilterAttribute 中获取请求正文?

转载 作者:行者123 更新时间:2023-12-02 02:32:26 25 4
gpt4 key购买 nike

我的请求示例

http://localhost:8065/api/note
POST
content-type:application/json
request body: { "id" : "1234", "title" : "test", "status" : "draft"}

响应应该是

{ "msg" : "ok", "code" : 1}

行动

public async Task<IActionResult> Post([FromBody]NoteModel model)
<小时/>

为了自动记录每个请求,我创建了一个属性来完成这项工作。该属性如下所示:(来自 Microsoft Docs )

public class SampleActionFilterAttribute : TypeFilterAttribute
{
public SampleActionFilterAttribute():base(typeof(SampleActionFilterImpl))
{
}

private class SampleActionFilterImpl : IActionFilter
{
private readonly ILogger _logger;
public SampleActionFilterImpl(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<SampleActionFilterAttribute>();
}

public void OnActionExecuting(ActionExecutingContext context)
{

}

public void OnActionExecuted(ActionExecutedContext context)
{
_logger.LogDebug("[path]" + context.HttpContext.Request.Path);
_logger.LogDebug("[method]" + context.HttpContext.Request.Method);
_logger.LogDebug("[body]"); //log request body, expectation: { "id" : "1234", "title" : "test", "status" : "draft"}
_logger.LogDebug("[statuscode]" + context.HttpContext.Response.StatusCode);
_logger.LogDebug("[response]"); //log response
}
}
}

我尝试使用streamReader来获取请求正文,但只得到空字符串。

StreamReader reader = new StreamReader(context.HttpContext.Request.Body);
string text = reader.ReadToEnd();

这是因为主体是由[fromBody]从 Controller 读取的,所以流不能被读取两次吗?如果是这样,我应该如何在 OnActionExecuted 方法中获取请求正文和响应?

<小时/>

更新:

我刚刚将 Set 的代码复制到我的项目中,但不起作用。这是调试 gif enter image description here

最佳答案

根据这个“Best way to log/read request body in a middleware ”线程,以下内容应该有效:

// using Microsoft.AspNetCore.Http.Internal;

public class SampleActionFilterAttribute : TypeFilterAttribute
{
...

public void OnActionExecuting(ActionExecutedContext context)
{
// read body before MVC action execution
string bodyData = ReadBodyAsString(context.HttpContext.Request);
}

private string ReadBodyAsString(HttpRequest request)
{
var initialBody = request.Body; // Workaround

try
{
request.EnableRewind();

using (StreamReader reader = new StreamReader(request.Body))
{
string text = reader.ReadToEnd();
return text;
}
}
finally
{
// Workaround so MVC action will be able to read body as well
request.Body = initialBody;
}

return string.Empty;
}
}

Read request body twice 中也描述了类似的方法所以帖子

<小时/>

更新:上述方法在ReadBodyAsString中如果在中间件中使用,则 with 将起作用,而不是在操作过滤器中。不同之处在于,当调用 Action 过滤器时(即使是 OnActionExecuting ),主体流已经被读取并且 [FromBody] model 。已填充。

好消息是,可以使用context.ActionArguments["<model_name>"]直接在 Action 过滤器中获取模型。对于您的情况:

public void OnActionExecuted(ActionExecutedContext context)
{
var model = context.ActionArguments["model"] as NoteModel;
}

关于c# - 有没有办法在.NET Core FilterAttribute 中获取请求正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499708/

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