我分離了一些代碼了一個網站的背後複製的代碼有問題的特定頁面後,我就PostAsJsonAsync()
行代碼得到一個錯誤:的HttpClient對象方法缺少
HttpResponseMessage response = await client.PostAsJsonAsync("api/...", user);
這在這個using語句(添加的報頭爲好)
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?)
即使它在以前的項目中工作,並從整個項目中直接複製。我忘了添加一些東西嗎?
我很感激任何關於此事的幫助。
賓果。只要它允許我將它標記爲答案。 –
等待您的代碼正常工作;) – Abhishek