title: calling async child action
body: <p>My ASP.NET MVC _Layout.cshtml has a call to a child action synchronous version of "AdminMenu" in the LayoutController which determines what menu option to be renderred.  In the child action, it calls asynchronously a remote webapi .  The problem is when the app starts the menu does not show, but referesh the screen once or twice the menu appears.  There is a delay in getting the response from WebApi I don't know how to get around it.  I thought async method would automatically update the menu when it completes, but it doesn't.</p>
  
  <p>Alternative, I try to call the aync version "AdminMenu2", I always get the following error.  Thank you for your help.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>    HttpServerUtility.Execute blocked while waiting for an asynchronous 
  operation to complete. Description: An unhandled exception occurred during 
  the execution of the current web request. Please review the stack trace for 
  more information about the error and where it originated in the code. 
  Exception Details: System.InvalidOperationException: HttpServerUtility.Execute 
  blocked while waiting for an asynchronous operation to complete. Source Error: 
  Line 50: &lt;/ul&gt;
  Line 51: &lt;ul class="nav navbar-nav navbar-right"&gt;
  Line 52:   @Html.Action("AdminMenu", new { Controller = "Layout" }) 
  Line 53: 
  Line 54:   &lt;li class="dropdown-toggle"&gt;
  
  
          Source File: c:\..\..\..\..\..\..\..\Views\Shared\_Layout.cshtml Line: 52</code></pre>
  </div>
  </div>
  </p>
  
  <pre class="lang-js prettyprint-override"><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net.Http;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Mvc;
  using Newtonsoft.Json;
  using myHelpersLib;
  using mycModel;
  
  namespace myMVC.Controllers {
    public class LayoutController: BaseController {
      [ChildActionOnly]
      public ActionResult AdminMenu() {
  
        string viewName = string.Empty;
        WebApiUserModel thisUser = new WebApiUserModel();
        if (Session["userProfile"] != null) {
          thisUser = (WebApiUserModel) Session["userProfile"];
        } else {
          //make webapi call to webapi/user 
          var response = GetUserProfile().ContinueWith(x =&gt; {
            //do something with the result
            if (x.IsCompleted) {
              thisUser = x.Result;
            }
          });
        }
  
        return PartialView(thisUser);
      }
  
      [ChildActionOnly]
      public async Task &lt; ActionResult &gt; AdminMenu2() {
        WebApiUserModel thisUser = new WebApiUserModel();
        if (Session["userProfile"] != null) {
          thisUser = (WebApiUserModel) Session["userProfile"];
        } else {
          HttpClient httpClient;
          HttpResponseMessage response;
  
          string svcLocation = System.Configuration.ConfigurationManager.AppSettings["WebApiEndpoint"];
          httpClient = myHelpersLib.WebApiBorker.GetClient(svcLocation);
          response = await httpClient.GetAsync("user");
          WebApiUserModel userProfile = new WebApiUserModel();
          if (response.IsSuccessStatusCode) {
            string content = await response.Content.ReadAsStringAsync();
            userProfile = JsonConvert.DeserializeObject &lt; WebApiUserModel &gt; (content);
            if (userProfile.Role != null)
              userProfile.Role = userProfile.Role.Trim();
          }
        }
  
        return PartialView(thisUser);
        //return new EmptyResult();
      }
  
      private async Task &lt; WebApiUserModel &gt; GetUserProfile() {
        HttpClient httpClient;
        HttpResponseMessage response;
  
        string svcLocation = System.Configuration.ConfigurationManager.AppSettings["fsrWebApiEndpoint"];
        httpClient = myHelpersLib.WebApiBorker.GetClient(svcLocation);
        response = await httpClient.GetAsync("user");
        WebApiUserModel userProfile = new WebApiUserModel();
        if (response.IsSuccessStatusCode) {
          string content = await response.Content.ReadAsStringAsync();
          userProfile = JsonConvert.DeserializeObject &lt; WebApiUserModel &gt; (content);
          if (userProfile.Role != null)
            userProfile.Role = userProfile.Role.Trim();
        }
        return userProfile;
      }
    }
  }
  </code></pre>