public class BodyReaderAttribute : ActionFilterAttribute
{
private string parameterName;
public BodyReaderAttribute(string parameterName = "content")
{
this.parameterName = parameterName;
}
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
var syncIOFeature = context.HttpContext.Features.Get<IHttpBodyControlFeature>();
if (syncIOFeature != null)
{
syncIOFeature.AllowSynchronousIO = true;
}
var request = context.HttpContext.Request;
request.EnableBuffering();
var reader = new StreamReader(request.Body);
string content = reader.ReadToEnd();
request.Body.Seek(0, SeekOrigin.Begin);
context.ActionArguments.Add(this.parameterName, content);
await base.OnActionExecutionAsync(context, next);
}
}