使用await Task async挂起MVC应用程序中的控制器

我有两个MVC网站。 站点1有一个控制器,使用以下代码调用站点2

// If I remove this in the controller of site1, then execution continues.... var asdf = SharedTypes.Utilities.GetjsonStream("http://localhost:11541/UIDP/Details/a1?format=json"); string g = asdf.Result; public class Utilities { public static async Task GetjsonStream(string url) { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); string content = await response.Content.ReadAsStringAsync(); Debug.WriteLine("Content: " + content); return content; } } 

我能够直接浏览URL并查看JSON ..但是从MVC中的同行网站下载JSON的正确方法是什么?

您应该将控制器方法转换为async方法并使用await来避免死锁 。

 public async Task MyActionAsync() { var asdf = SharedTypes.Utilities.GetjsonStream(someUrl); string g = await asdf; // return something } 

Microsoft的ASP.NET教程包含一个关于ASP.NET MVC 4中异步方法的页面。