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

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.

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

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 .net core

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 [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.

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

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

does not contain a definition for are you missing a using directive or an assembly reference

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
cs1061: 'are you missing a using directive or an assembly reference?)

Alternatively, you need to add 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

httpcontext' does not contain a definition for 'current

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

You may like following the below Articles

Conclusion

Well, in this article, we discussed 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 I got while creating an Azure Function using the Visual Studio 2019. Hope it will fix your issue !!!

Leave a Comment