I have web API in .net core3.
In the filter I need to get the request body
public override void OnActionExecuting(ActionExecutingContext context)
{
string body = ReadBodyAsString(context.HttpContext.Request);
}
private string ReadBodyAsString(HttpRequest request)
{
var initialBody = request.Body; // Workaround
try
{
request.EnableBuffering();
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;
}
I get the following error:
Cannot access a disposed object.\r\nObject name: 'FileBufferingReadStream`
any help is appreciated