【发布时间】:2018-08-09 03:50:56
【问题描述】:
由于缺乏管理线程的经验,我遇到了一个问题。 我在下面有这个动作:
public static async Task<joueurs> loadjoueurs(int id)
{
joueurs EmpInfo = new joueurs();
using (var client = new HttpClient())
{
//Passing service base url
client.BaseAddress = new Uri("http://www.myWebApi.fr/api/");
client.DefaultRequestHeaders.Clear();
//Define request data format
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Sending request to find web api REST service resource GetAllEmployees using HttpClient
HttpResponseMessage Res = await client.GetAsync("joueurs?id=" + id);
//Checking the response is successful or not which is sent using HttpClient
if (Res.IsSuccessStatusCode)
{
//Storing the response details recieved from web api
var EmpResponse = Res.Content.ReadAsStringAsync().Result;
//Deserializing the response recieved from web api and storing into the Employee list
EmpInfo = JsonConvert.DeserializeObject<joueurs>(EmpResponse);
return EmpInfo;
}
return null;
}
只是客户端从 webApi 获取我的数据(没有 ssl 没有身份验证,当我测试它时,我收到了正确的值) 但是当我使用上面的函数(在我的 asp.net 网站中)拨打电话时....它永远停留在 HttpResponseMessage = await ....。
在我的 webApi 中,我有两个同名但参数不同的函数。
public async Task<IHttpActionResult> Getjoueur(int iduser, int idsport)
和
public async Task<IHttpActionResult> Getjoueur(int id)
所以我不知道问题出在哪里。
(续集)这里是我调用任务的地方:
public SuperModel(int id)
{
this.joueur = Repojoueurs.loadjoueurs(id).Result;
/* this.classificationSport = Repoclassificationsport.loadclassificationsport().Result;
...
*/
}
然后我的 Supermodel 在我的 Home 控制器中被实例化:
public ActionResult Index(int id)
{
SuperModel superModel = new SuperModel(id);
return View(superModel);
}
【问题讨论】:
-
关于如何以及在何处调用 Task
loadjoueurs(int id) 的任何信息? -
... 显然使用 Fiddler 我知道我的 webApi 返回了预期的数据。所以我不知道为什么它一直卡在这里。
-
是的,当然,我在“Myclass”(这是我传递给返回 View('Myclass') 的模型)的构造函数中调用该方法。 ...在返回视图的操作中。
标签: c# asp.net asp.net-web-api httpclient httpresponsemessage