Post方法中的Web api客户端调用错误

时间:2016-10-19 08:01:10

标签: asp.net-web-api

string Url = ConfigurationManager.AppSettings["DataUrl"];
DateTime trndate = DateTime.Now.Date;     
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync(Url +"?id="+ trndate.ToString("yyyy-MM-dd")).Result;


上述方法成功但后期方法无效。我的帖子方法是调用代码。

HttpClient Postclient = new HttpClient();
Postclient.BaseAddress = new Uri(Url);
Postclient.DefaultRequestHeaders.Accept.Clear();
 Postclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
PostRemote rr = new PostRemote();
rr.id= 123;
rr.age= 38;
HttpResponseMessage postresponse = Postclient.PostAsJsonAsync(Url, rr).Result;


出现异常错误消息

"Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.**

1 个答案:

答案 0 :(得分:1)

这个问题已经多次面临过,只需要一个简单的解决方案。

转到工具> NuGet包管理器和选择包管理器控制台。执行以下两个命令:

uninstall-package newtonsoft.json -force

install-package newtonsoft.json

web.config

中添加/更改代码
 <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
     <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="9.0.0.0"/>
 </dependentAssembly>