How to fix HttpClient' does not contain a definition for 'DefaultRequestHeaders' error ?

I'm getting below error while compiling the below code

HttpClient' does not contain a definition for 'DefaultRequestHeaders'
HttpClient' does not contain a definition for 'GetAsync'

Project details:
Microsoft Visual C# 2012
.Net framework 4.5.2
Windows service

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;

var client = new HttpClient();
 client.BaseAddress = new Uri(ConfigurationManager.AppSettings["uri-path-port"].ToString());

 client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
           

Open in new window


HttpClient' does not contain a definition for 'DefaultRequestHeaders'
HttpClient' does not contain a definition for 'GetAsync'

To fix this issue I  have tried the below  form package manager console:

Install-Package Microsoft.AspNet.WebApi.Client

I got this error:

Install-Package : Unable to find package 'Microsoft.AspNet.WebApi.Client'.
At line:1 char:1
+ Install-Package Microsoft.AspNet.WebApi.Client
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Install-Package], InvalidOperationException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand
Varshini SAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPRetiredCommented:
This Builds Correctly  
Install-Package Microsoft.AspNet.WebApi.Client isn't a nuget package use the website properties add reference instead.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Http.Headers;
using System.Net;
using Newtonsoft.Json;
using System.Net.Http;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri(ConfigurationManager.AppSettings["uri-path-port"].ToString());

        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));




    }
}

Open in new window

Varshini SAuthor Commented:
Hi David,

The reference has been already added.
reference.PNG
kaufmed   ( ͡° ͜ʖ ͡°)*whispers*  I C# people.Commented:
What version of the library are you using?
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Varshini SAuthor Commented:
version.png
kaufmed   ( ͡° ͜ʖ ͡°)*whispers*  I C# people.Commented:
What version of .NET are you targeting?
Varshini SAuthor Commented:
Framework 4.5.2
kaufmed   ( ͡° ͜ʖ ͡°)*whispers*  I C# people.Commented:
I'm at a loss then. Those versions should be fine, and the only time I've seen stuff like this is when an older version of either is being targeted/referenced.
kaufmed   ( ͡° ͜ʖ ͡°)*whispers*  I C# people.Commented:
If you hover over HttpClient, is it showing the correct namespace?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Varshini SAuthor Commented:
Yes. it's showing the correct namespace.
Varshini SAuthor Commented:
Thank You Kaufmed and David. The problem got fixed when I changed the target framework to 4.5 
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.