本文介绍了AJAX Helper和async/await之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在观看Scott Allen在Plural Sight上的教程视频时:

While reviewing the tutorial videos by Scott Allen on Plural Sight:

使用ASP.NET MVC 4和 ASP.NET MVC 5基础

我遇到了两个不同的术语:AJAX Helperasync/await.

I came across 2 different terms: AJAX Helper and async/await.

对我来说,这听起来"都是在异步模式下运行(如果我输入错了,请纠正我).两者之间到底有什么区别?

It "sounds" to me both operate on the asynchronous mode (correct me if I'm wrong). What exactly is the difference between each?

推荐答案

它们实际上完全不同.理解差异的关键是考虑HTTP协议.

They are actually completely different. The key to understanding the difference is to think about the HTTP protocol.

在HTTP协议中,您有一个请求和一个响应.而已.客户端发出请求,服务器创建并发送响应.

In the HTTP protocol, you have one request and one response. That's it. The client makes a request, and the server creates and sends a response.

在请求处理程序(例如MVC操作)中使用asyncawait时,您仍然只有一个响应. async不会更改HTTP协议.因此,async/await的作用是在完成异步工作的同时释放请求线程.但是,在完成异步工作的同时,没有任何内容发送到客户端(尚未创建响应).您可以将async/await视为对ASP.NET运行时的屈服",而不是客户端浏览器.

When you use async and await in a request handler (such as an MVC action), you still only have one response. async doesn't change the HTTP protocol. So, what async/await does is free up the request thread while there is asynchronous work being done. But while that asynchronous work is being done, nothing is sent to the client (the response is not yet created). You can think of async/await as "yielding" to the ASP.NET runtime, but not the client browser.

AJAX是另一种方法. AJAX是用于从浏览器发出附加 HTTP请求而不更改当前页面的约定.如果您要编写SPA,或者说要加载占位符"页面并然后加载内容,则可以使用AJAX.

AJAX is a different approach. AJAX is a convention for issuing additional HTTP requests from a browser, without changing the current page. You'd use AJAX if you're writing a SPA, or say, if you wanted to load a "placeholder" page and then load the content.

有关更多信息,请参见我在异步ASP.NET上的MSDN文章.

For more information, see the "Asynchronous Code Is Not a Silver Bullet" section of my MSDN article on async ASP.NET.

这篇关于AJAX Helper和async/await之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:09