我想打電話從我的網站application.Am使用.NET 4.5的web API和一邊寫代碼我得到一個錯誤的HttpClient不包含定義PostAsJsonAsync方法HttpClient的不支持PostAsJsonAsync方法C#
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:51093/");
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
var user = new Users();
user.AgentCode = 100;
user.Remarks = "Test";
user.CollectionDate = System.DateTime.Today;
user.RemittanceDate = System.DateTime.Today;
user.TotalAmount = 1000;
user.OrgBranchID = 101;
var response = client.PostAsJsonAsync("api/AgentCollection", user).Result;
,我收到錯誤消息:
Error: '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?)
請看看,並建議我。
最好的辦法是增加「Microsoft.AspNet.WebApi.Client」 .Nuget包。而已! –