c# - 具有异步和等待的 MVC 操作

标签 c# asp.net-mvc asp.net-mvc-4 async-await

我在 MVC3 应用程序(VS2012 中的空应用程序)中有以下操作,它只加载一个只有一个 H2 元素的索引页面。当然这不会做任何事情,但我只是在学习异步和等待功能。

索引页面未加载。我只是在浏览器中得到这个。

System.Threading.Tasks.Task`1[System.Web.Mvc.ActionResult]

同样在 fiddler 中,我收到了蓝色的请求。我做错了什么。为什么 View 没有显示。

异步调用确实成功并显示了网站中的html内容。

查看代码:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>


public async Task<ActionResult> Index()
        {
            Task<string> getContentsTask = GetContentsAsync();
            Thread.Sleep(10000);
            string contents = await getContentsTask;
            return View("Index");
        }

        async Task<string> GetContentsAsync()
        {

            HttpClient client = new HttpClient();
            Task<string> contents = client.GetStringAsync("http://www.msdn.com");

            string data = await contents;
            return data;
        }

最佳答案

MVC3 无法识别Task

升级到 MVC4。

关于c# - 具有异步和等待的 MVC 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19098759/

相关文章:

c# - 如何模拟/创建 X509Certificate2 进行单元测试?

c# - 自定义配置文件提供程序 : The settings property '' was not found - MVC3

unit-testing - 单元测试 MVC 身份验证 VS 2013

asp.net - MVC中两个数相加

c# - 需要为最大日期值构建表达式树

c# - WPF:如何获得形状的真实大小(边界框)

c# - 在 DataGridView 中以编程方式选择行

asp.net - 为什么 MetadataType 不在数据库优先方法中进行验证?

javascript - 重构 JS 对象以适应绑定(bind) MVC 模型

asp.net - 为什么 Breeze/Knockout 模板作为工具而不是模板安装,使其无法使用?