Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

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
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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

Avatar of Varshini S
Varshini S

ASKER

Hi David,

The reference has been already added.
User generated image
What version of the library are you using?
What version of .NET are you targeting?
Framework 4.5.2
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.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes. it's showing the correct namespace.
Thank You Kaufmed and David. The problem got fixed when I changed the target framework to 4.5