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.
Alternative, I try to call the aync version "AdminMenu2", I always get the following error. Thank you for your help.
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: </ul>
Line 51: <ul class="nav navbar-nav navbar-right">
Line 52: @Html.Action("AdminMenu", new { Controller = "Layout" })
Line 53:
Line 54: <li class="dropdown-toggle">
Source File: c:\..\..\..\..\..\..\..\Views\Shared\_Layout.cshtml Line: 52
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 => {
//do something with the result
if (x.IsCompleted) {
thisUser = x.Result;
}
});
}
return PartialView(thisUser);
}
[ChildActionOnly]
public async Task < ActionResult > 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 < WebApiUserModel > (content);
if (userProfile.Role != null)
userProfile.Role = userProfile.Role.Trim();
}
}
return PartialView(thisUser);
//return new EmptyResult();
}
private async Task < WebApiUserModel > 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 < WebApiUserModel > (content);
if (userProfile.Role != null)
userProfile.Role = userProfile.Role.Trim();
}
return userProfile;
}
}
}