I'm migrating a.NET 4.8 web API application to 6.0 but the snippet used to retrieve raw body content within a controller's method no longer works and always returns an empty string. By raw content I mean the whole json structure that is used to write a custom log. The.net 4.8 code, which runs within a post method, is as follows:
public async Task<IActionResult> PostAsync([FromBody] FooModel model)
{
// some code
string rawContent = string.Empty;
using (var st = await this.Request.Content.ReadAsStreamAsync())
{
st.Seek(0, System.IO.SeekOrigin.Begin);
using (var sr = new System.IO.StreamReader(st))
{
rawContent = sr.ReadToEnd();
}
}
// some more code
}
In.net 6 I adapted it like this:
string rawContent = string.Empty;
using (var reader = new StreamReader(Request.Body,
encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: false))
{
rawContent = await reader.ReadToEndAsync();
}
Despite some adaptations I can not recover the data. Is there anyone who can give me directions? Thank you.
ste22