c# – HttpClient对象方法丢失

我将一些代码一个网站中分离出来,并且在复制相关特定页面后面的代码后,我在PostAsJsonAsync()代码行上发生错误
HttpResponseMessage response = await client.PostAsJsonAsync("api/...",user);

这是在这个使用声明(添加标题)

using System;
        using System.Net.Http;
        using System.Net.Http.Headers;
        using System.Net.Mail;
        using System.Threading.Tasks;
        //...

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("WebServiceAddress");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpResponseMessage response = await client.PostAsJsonAsync("api/...",user);
            if (response.IsSuccessstatusCode)
            {
                const string result = "Thank you for your submission.";
                return result;
            }
            //...
        }

我得到的错误

Error  4   'System.Net.Http.HttpClient'
 does not contain a deFinition for 'PostAsJsonAsync' and no extension
 method 'PostAsJsonAsync' accepting a first argument of type 'System.Net.Http.HttpClient'
 Could be found (are you missing a using directive or an assembly reference?)

即使它在前一个项目中工作,并从该项目中直接复制.我忘了添加东西吗?

我感谢任何有关此事的帮助.

解决方法

您将不得不添加以下依赖关系,
System.Net.Http.Formatting.dll

它应该在扩展名中 – >部件.

要么

您可以添加Microsoft.AspNet.WebApi.Client nuget包

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...