Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am re-writing a POST method from ASP.NET Web API to ASP.NET Core I am not sure how to re-write the below code. This code works fine in ASP.NET Web API.

ASP.NET Web Api:

public IHttpActionResult Post()
{
    var rawds = ConvertByteToArray(Request.Content.ReadAsStreamAsync().Result); //error here at Content
    //do something
}

private byte[] ConvertByteToArray(Stream stream)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                stream.CopyTo(ms);
                return ms.ToArray();
            }
        }


What I have tried:

When I write the same code as below in ASP.NET Core I get the following error 'HttpRequest' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)

ASP.NET Core:

public IActionResult Post()
{
    var rawds = ConvertByteToArrayAsync(Request.Content.ReadAsStreamAsync().Result);
    //do something
} 

public static async Task<byte[]> ConvertByteToArrayAsync(Stream stream)
        {
            using (var ms = new MemoryStream(2048))
            {
                await request.Body.CopyToAsync(ms); //get error here at request
                return ms.ToArray();
            }
        }
Posted
Updated 8-Jul-18 19:54pm
v2

1 solution

C#
await request.Body.CopyToAsync(ms);

1. In any case it should be Request (R and not r)!
2. It seems to be a logic error, that you pass the 'Result' stream to the function but do not use it? You probably meant to copy it to ms?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900