HttpClient的不支持PostAsJsonAsync方法C# [英] HttpClient not supporting PostAsJsonAsync method C#

查看:9133
本文介绍了HttpClient的不支持PostAsJsonAsync方法C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从使用.NET 4.5我的网络application.Am调用Web API,并在写入code我得到一个错误的HttpClient不包含定义PostAsJsonAsync方法

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

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?)

请看看,并建议我。

推荐答案

是的,你需要添加一个参考。

Yes, you need to add a reference to

System.Net.Http.Formatting.dll

这可以在扩展组件区域被发现。

This can be found in the extensions assemblies area.

这篇关于HttpClient的不支持PostAsJsonAsync方法C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆