title: How do I call a method that returns a Task<T> From an ASP.Net MVC4 Controller Method?
body: <p><strong>TL;DR:</strong> I would like to know how I can get the return value from a method that returns <code>Task&lt;T&gt;</code> when invoked from an <strong>ASP.Net MVC4</strong> Controller's <code>ActionResult</code> method. Code snippets below.</p>
  
  <p><strong>Question Details:</strong><br/>
  I'm using the <a href="https://github.com/burkeholland/instasharp" rel="nofollow">Instasharp Library</a> on <strong>github</strong>.</p>
  
  <p>Here's the <code>Search</code> method that returns <code>Task&lt;UserResponse&gt;</code>. This is defined in the Instasharp Library.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Search for a user by name.
  /// &lt;para&gt;
  /// &lt;c&gt;Requires Authentication: False&lt;/c&gt;
  /// &lt;/para&gt;
  /// &lt;/summary&gt;
  /// &lt;param name="searchTem"&gt;A query string.&lt;/param&gt;
  /// &lt;param name="count"&gt;Number of users to return.&lt;/param&gt;
  /// &lt;returns&gt;UsersResponse&lt;/returns&gt;
  public Task&lt;UsersResponse&gt; Search(string searchTerm, int? count = null) {
      var request = base.Request("search");
  
      request.AddParameter("q", searchTerm);
      request.AddParameter("count", count);
  
      return base.Client.ExecuteAsync&lt;UsersResponse&gt;(request);
  }
  </code></pre>
  
  <p>Here's a Unit Test in the same library project's source code, that works. If I put a breakpoint at the <code>Assert</code> line and inspect the <code>result</code>, it contains an instance of <code>List&lt;UserResponse&gt;</code> with data from the Instagram API.</p>
  
  <pre><code>    [TestMethod, TestCategory("Users.Search")]
      public async Task Search()
      {
          var result = await users.Search("shiva");
          Assert.IsTrue(result.Data.Count &gt; 0);
      }
  </code></pre>
  
  <p>In my ASP.Net MVC4 Application's <code>HomeController</code>, in the <code>Index</code> method that will query the instagram API and return a strong type <code>List&lt;Instasharp.Models.User&gt;</code> (which is contained in the <code>Data</code> property of the <code>UserResponse</code> object) to the corresponding View, the code is as follows. ( Note: This is per the documentation on the Github page and is supposed to work.).</p>
  
  <pre><code>   public class HomeController : Controller
      {
          public ActionResult Index()
          {
              var userList = new List&lt;InstaSharp.Models.User&gt;();
              var mediaList = new List&lt;InstaSharp.Models.Media&gt;();
              var followingList = new List&lt;InstaSharp.Models.User&gt;();
              try
              {
                   var config = new InstaSharp.InstagramConfig(
                       clientId:"MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM",
                       clientSecret:"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
  
                   InstaSharp.Endpoints.Users users = new InstaSharp.Endpoints.Users(config);
                    var result = users.Search("shiva");
                    userList = result.Result.Data;
              }
              catch (Exception ex)
              {
                  ViewBag.Message = ex.Message + "\n\n" + ex.StackTrace.ToString();
              }
  
              return View(userList);
          }
      }
  </code></pre>
  
  <p><strong>The Problem:</strong><br/> My code is hanging (i.e. <code>localhost:portnumber</code> is just waiting.). There are no issues authenticating with Instagram API. The line</p>
  
  <p><code>var result = users.Search("shiva");</code></p>
  
  <p>is showing that result is a <code>Task</code>. I am not sure how to invoke it and get the <code>Data</code> in the return value from the <code>users.Search("shiva")</code> method.</p>
  
  <p><strong>What I have Tried:</strong><br/>
  I've tried adding the <code>await</code> in front of the <code>users.Search("shiva")</code> call. Then the compiler is asking me to make the <code>ActionResult Index()</code> also <code>async</code> and wrap a <code>Task&lt;..&gt;</code> around it. When I did that also, the code is hanging. 
  I also tried looking on MSDN <a href="http://www.asp.net/mvc/tutorials/mvc-4/using-asynchronous-methods-in-aspnet-mvc-4" rel="nofollow">at this tutorial</a>, but that didn't help either.
  I've spent quite a few hours on this, and am hoping someone on SO can help me.</p>
  
  <p>[Note: This is not production code. I am simply trying to get the Instasharp library working in an ASP.Net MVC4 Controller and display data in a View. So please don't get fixated on coding practices etc. Also, I've faked out the <code>ClientId</code> and <code>ClientSecret</code> for the question. I don't think you would need it to answer this question.]</p>