c# – HttpClient对象方法丢失

前端之家收集整理的这篇文章主要介绍了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包

原文链接:https://www.f2er.com/csharp/95484.html

猜你在找的C#相关文章