我将一些代码从一个网站中分离出来,并且在复制相关特定页面后面的代码后,我在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?)
即使它在前一个项目中工作,并从该项目中直接复制.我忘了添加东西吗?
我感谢任何有关此事的帮助.