加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – HttpClient对象方法丢失

发布时间:2020-12-15 06:28:15 所属栏目:百科 来源:网络整理
导读:我将一些代码从一个网站中分离出来,并且在复制相关特定页面后面的代码后,我在PostAsJsonAsync()代码行上发生错误: HttpResponseMessage response = await client.PostAsJsonAsync("api/...",user); 这是在这个使用声明(添加的标题) using System; using Sys
我将一些代码从一个网站中分离出来,并且在复制相关特定页面后面的代码后,我在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包

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读