title: How to use async Task to get data and show in a view?
body: <p>I have a web API and i want to get some data in a controller and show on a view.</p>
  
  <p>this is my main class:</p>
  
  <pre><code>public class APISrv
  {
     public string result = string.Empty;
      public async Task&lt;string&gt; GetIT()
      {
          try
          {
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri("http://localhost:2474/");
                  client.DefaultRequestHeaders.Clear();
                  client.DefaultRequestHeaders.Accept.Add(new 
                   MediaTypeWithQualityHeaderValue("application/json"));
  
                  HttpResponseMessage Res = await 
                  client.GetAsync("api/Test/Get");
  
                  if (Res.IsSuccessStatusCode)
                  {
                      result = Res.Content.ReadAsStringAsync().Result;
                  }
              }
  
          }
          catch (Exception err)
          {
              result=err.Message;
          }
          return result;
      }
  }
  </code></pre>
  
  <p>i tested my class with two controllers:</p>
  
  <p>one:</p>
  
  <pre><code>public ActionResult Index()
      {
          APISrv aPISrv = new APISrv();
          aPISrv.GetIT();
          ViewData["mydata"] = aPISrv.result;
          return View();
      }
  </code></pre>
  
  <p>in this cause "result" is a public field and always is empty,it seems main thread does not wait for async method done its work.i can see my view but "ViewData["mydata"]" is empty.</p>
  
  <p>on second:</p>
  
  <pre><code>public ActionResult Index()
      {
          APISrv aPISrv = new APISrv();
          ViewData["mydata"] = aPISrv.GetIT().Result;
          return View();
      }
  </code></pre>
  
  <p>in this cause my page stay in "waiting for local..." mode and does not work anything.</p>
  
  <p>how can i and what is the best way to get data from my class?</p>