2

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

ste22
  • 33
  • 1
  • 4

2 Answers2

6

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.

Well, I have simulate the issue on dotnet 6 Web API project in following way, and I am getting the json which I am sending from PostMan

Controller:

    [HttpPost]
    public async Task<IActionResult> GetRawContent()
    {
        string rawContent = string.Empty;
        using (var reader = new StreamReader(Request.Body,
                      encoding: Encoding.UTF8, detectEncodingFromByteOrderMarks: false))
        {
            rawContent = await reader.ReadToEndAsync();
        }
       
        return Ok(rawContent);
       
    }

Output:

enter image description here

Simulation:

enter image description here

Note: As you are sending raw content so you can get rid of[FromBody] FooModel model as this only used when we explicitely use stricly typed model

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • this suggestion is also good; thanks for everything. – ste22 Nov 02 '22 at 11:30
  • I want to know why ASP.NET makes this so hard. It should be an utterly standard and trivially easy thing to do: there are situations where, for example, you are trying to handle a range of different object types, OR you are just trying to work out why the mapping isn't working the way you think it ought to. – Robbie Matthews May 29 '23 at 08:07
  • Hey, thanks for your response, not sure what's your concern and the context even what seems to your harder. In addition, It would be great if you could share your scenario in separate question thread so that it can be investigated accordingly. On top of this, based on scenario, reading request body may differ as numerous approach out there. – Md Farid Uddin Kiron May 29 '23 at 08:45
  • Right: Real world: I was chasing a problem where sometimes a POST was just not receiving anything in the body, just a null. It turned out there was ONE field in the json coming in as null, and the model definition did not allow for it. There was no meaningful error message anywhere I could find. Being able to examine what I was actually seeing would have made life much easier. – Robbie Matthews May 29 '23 at 09:26
  • Okay, thanks for the clarification, its depends on requirement and the approach. – Md Farid Uddin Kiron May 29 '23 at 09:52
-1

For 4.8 System.Web.Http.ApiController, this mostly works:

[HttpPost]
[Route("api/values/getrawcontent")]
public async Task<string> GetRawContent(int tag)
{
    string rawContent = await Request.Content.ReadAsStringAsync();
    return $"{tag}:rawContent}";
}

I say MOSTLY works, because all special characters seem to be escaped.

Robbie Matthews
  • 1,404
  • 14
  • 22