我的要求样本

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 c# - 有什么方法可以获取.NET Core FilterAttribute中的请求正文?-LMLPHP

最佳答案

对应于这个“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 SO post中也描述了类似的方法

更新:如果在中间件中(而不是在 Action 过滤器中)使用上述ReadBodyAsString中的方法将起作用。区别在于,在调用 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/

10-17 01:43