【问题标题】:c# asp.net website calling a webApic# asp.net网站调用一个webApi
【发布时间】: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


【解决方案1】:

你能不能尽量不使用asyncwait。大约三个变化,如下所示

public static HttpResponseMessage loadjoueurs(int id)
    {

            HttpResponseMessage Res = client.GetAsync("joueurs?id=" + id);

return Request.CreateResponse(HttpStatusCode.OK,EmpInfo, "application/json");
     }

【讨论】:

  • HttpClient 只提出异步方法......坦率地说,异步在我的项目中非常重要。
  • 致敬!我找到了!这是解决方案: HttpResponseMessage Res = await client.GetAsync("joueurs?id=" + id).ConfigureAwait(false);
  • 很高兴您找到了它。另一方面,你是对的 HttpClient 只提出异步,但是,你仍然可以在没有等待的情况下调用它。祝你好运。
猜你喜欢
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 2014-08-25
相关资源
最近更新 更多