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

标签 c# asp.net-core asp.net-core-mvc .net-core

我的请求示例

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/

相关文章:

c# - 线程无法访问对象

c# - 在 C# (ASP.MVC) 中将 Access DB (.mdb) 与 ADODB 一起使用,就像使用经典 ASP

c# - 如何为实现接口(interface)的每个对象设置属性的默认值?

c# - 使用 Blazor Asp.Net Core 托管模板时获取 "NETSDK1045 The current .NET SDK does not support .NET Core 3.0 as a target"

asp.net-core - .NET Core 运行时无需安装

c# - 我可以先对数据库使用覆盖 OnModelCreating 方法吗

azure - Azure 应用服务上的 Asp.Net Core 2.2 TLS 1.2 支持

c# - Ef Core 中的数学绝对值?

c# - 区域和子域路由

Azure部署: Could not load file or assembly 'dnx.clr.managed' or one of its dependencies.系统找不到指定的文件