I have an error logger that writes errors from an API controller to the database. It has some code like this:
string form = string.Empty;
if (request.Method == "POST")
{
using (StreamReader sr = new StreamReader(request.Body))
{
if (request.Body.CanSeek) request.Body.Seek(0, SeekOrigin.Begin);
if (request.Body.CanRead) form = sr.ReadToEndAsync().Result;
form = sr.ReadToEndAsync().Result;
}
}
However, this can't read the body because it's already been read, and ASP.NET Core doesn't let you do this. See: https://devblogs.microsoft.com/aspnet/re-reading-asp-net-core-request-bodies-with-enablebuffering/
I looked at .EnableBuffering
and .EnableRewind
but neither of these exist on HttpRequest
(at least not in 3.0.
app.Use(async (context, next) => {
context.Request.EnableRewind();
await next();
});
How do I get to reread the request body?