在 WebApi Core ActionFilter 中将请求正文作为字符串获取?
Get Request Body as String in WebApi Core ActionFilter?
以下是我在 WebApi .Net Core 2 项目中的 ActionFilter:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequestLoggingAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var request = actionContext.HttpContext.Request;
var route = request.Path.HasValue ? request.Path.Value : "";
var requestHeader = request.Headers.Aggregate("", (current, header) => current + $"{header.Key}: {header.Value}{Environment.NewLine}");
request.EnableRewind();
var requestBody = new StreamReader(request.Body).ReadToEnd();
}
}
requestHeader
有正确的值,没问题,但 requestBody
总是空的。
我通过将带有以下正文的请求发送到操作来测试它。
[
{
"TrackingCode": "96003445",
"Description": "",
"InnerMessage": "",
"Status": 11
},
{
"TrackingCode": "96003840",
"Description": "",
"InnerMessage": "Inner message",
"Status": 11
}
]
如何在 WebApi Core 2 ActionFilter 中获取请求主体?
通过@Nkosi 的指导,我找到了如下解决方案:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequestLoggingAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var request = actionContext.HttpContext.Request;
var route = request.Path.HasValue ? request.Path.Value : "";
var requestHeader = request.Headers.Aggregate("", (current, header) => current + $"{header.Key}: {header.Value}{Environment.NewLine}");
var requestBody = "";
request.EnableRewind();
using (var stream = new StreamReader(request.Body))
{
stream.BaseStream.Position = 0;
requestBody = stream.ReadToEnd();
}
}
}
效果很好
以下是我在 WebApi .Net Core 2 项目中的 ActionFilter:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequestLoggingAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var request = actionContext.HttpContext.Request;
var route = request.Path.HasValue ? request.Path.Value : "";
var requestHeader = request.Headers.Aggregate("", (current, header) => current + $"{header.Key}: {header.Value}{Environment.NewLine}");
request.EnableRewind();
var requestBody = new StreamReader(request.Body).ReadToEnd();
}
}
requestHeader
有正确的值,没问题,但 requestBody
总是空的。
我通过将带有以下正文的请求发送到操作来测试它。
[
{
"TrackingCode": "96003445",
"Description": "",
"InnerMessage": "",
"Status": 11
},
{
"TrackingCode": "96003840",
"Description": "",
"InnerMessage": "Inner message",
"Status": 11
}
]
如何在 WebApi Core 2 ActionFilter 中获取请求主体?
通过@Nkosi 的指导,我找到了如下解决方案:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class RequestLoggingAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext actionContext)
{
var request = actionContext.HttpContext.Request;
var route = request.Path.HasValue ? request.Path.Value : "";
var requestHeader = request.Headers.Aggregate("", (current, header) => current + $"{header.Key}: {header.Value}{Environment.NewLine}");
var requestBody = "";
request.EnableRewind();
using (var stream = new StreamReader(request.Body))
{
stream.BaseStream.Position = 0;
requestBody = stream.ReadToEnd();
}
}
}
效果很好