作者:丢失的面包树 | 来源:互联网 | 2022-12-13 15:47
Iamtryingtocallawebapifrommywebapplication.Amusing.net4.5andwhilewritingthecodei
I am trying to call a web api from my web application.Am using .net 4.5 and while writing the code i am getting an error HttpClient does not contain a definition PostAsJsonAsync method
我正在尝试从我的web应用程序调用web api。正在使用。net 4.5,在编写代码时,我得到一个错误HttpClient不包含定义PostAsJsonAsync方法
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;
and I am getting the error message :
我得到了错误信息:
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?)
Please have a look and advice me.
请给我看看,并给我一些建议。
7 个解决方案