HttpRequest does not contain a definition for content

In this Azure tutorial, we will discuss how to fix the error. CS1061 C# ‘HttpRequest’ does not contain a definition for ‘Content’ and no accessible extension method ‘Content’ accepting a first argument of type ‘HttpRequest’ could be found which comes while trying to create an Azure Function using Visual Studio 2019.

HttpRequest does not contain a definition for content

Recently, I was developing an Azure Function using Visual Studio 2019 and while executing the function i got the error CS1061 C# ‘HttpRequest’ does not contain a definition for ‘Content’ and no accessible extension method ‘Content’ accepting a first argument of type ‘HttpRequest’ could be found.

I was trying to execute the below C# function as part of Azure Function creation functionality.

 public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log, [Queue("EmpInfo", Connection = "AzureWebJobsStorage")] IAsyncCollector<UserDetails> outputQueue)
        {
            
            string jsoncnt = await req.Content.ReadAsStringAsync();
            var myDetails = JsonConvert.DeserializeObject<UserDetails>(jsoncnt);
            log.LogInformation($"Appreciation {myDetails.UserId} received from {myDetails.UserEmail} for the emp id {myDetails.UserId}");
            await outputQueue.AddAsync(myDetails);


            log.LogInformation("C# HTTP trigger function processed a request.");

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            return name != null
                ? (ActionResult)new OkObjectResult($"Hello, {name}")
                : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
        }
    }

I got the error on the below line

string jsoncnt = await req.Content.ReadAsStringAsync();

The exact error message was CS1061 C# ‘HttpRequest’ does not contain a definition for ‘Content’ and no accessible extension method ‘Content’ accepting a first argument of type ‘HttpRequest’ could be found (are you missing a using directive or an assembly reference?)

You can see as below

httprequest does not contain a definition for content

HttpRequest does not contain a definition for content [Solved]

Now to fix this error you need to follow the below steps.

Right click on the Project and click on the Manage NuGet Packages.

task httpresponsemessage does not contain a definition for content

You can search for Microsoft.AspNet.WebApi.Client and then click on the install button to install it.

httprequest does not contain a definition for createresponse

Or you can open the Nuget Manager console and execute the below command to install Microsoft.AspNet.WebApi.Client.

Install-Package Microsoft.AspNet.WebApi.Client
httprequest does not contain a definition for url

Alternatively, you need to add a reference the System.Net.Http.Formatting.dll to your project.

You need to right-click on your project —> Add —> Reference

Now Browse or search for the below dll and then click on the OK button to add the DLL reference to your project

method does not contain a definition for post

If you will implement the above process, then it will fix this error.

You may like