常规方法和异步方法(OnGet与OnGetAsync)之间有什么区别? [英] What is the difference between regular and Async methods (OnGet vs OnGetAsync)

查看:53
本文介绍了常规方法和异步方法(OnGet与OnGetAsync)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Razor Pages的工作方式,教程中提到了OnGet和OnPost,还提到了我们也有异步选项:OnGetAsync和OnPostAsync.但是他们没有提到它们是如何工作的,显然它们是异步的,但是如何?他们使用AJAX吗?

I started learning how Razor Pages work, tutorials mention OnGet and OnPost, and also mention that we have async options too: OnGetAsync and OnPostAsync. But they don't mention how they work, obviously they're asynchronous, but how? do they use AJAX?

public void OnGet()
{
}


public async Task OnGetAsync()
{
}

推荐答案

OnGet OnGetAsync 之间没有实际区别. OnGetAsync 只是包含异步代码的方法的命名约定,该异步代码应在发出GET请求时执行.您可以省略 Async 后缀,但仍使该方法异步:

There is no actual difference between OnGet and OnGetAsync. OnGetAsync is just a naming convention for methods that contain asynchronous code that should be executed when a GET request is made. You can omit the Async suffix but still make the method asynchronous:

public async Task OnGet()
{
    ...
    await ....
    ...
}

异步方法是在执行过程中释放其线程的方法,以便可以将其用于其他用途,直到执行结果可用为止.您可以在此处详细了解异步方法的工作方式:

Asynchronous methods are ones that free up their threads while they are executing so that it can be used for something else until the result of the execution is available. You can read more about how asynchronous methods work here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/#BKMK_WhatHappensUnderstandinganAsyncMethod

同一剃刀页面中不能有 Onget 处理程序.该框架认为它们是相同的.

You can't have an Onget and an OnGetAsync handler in the same Razor Page. The framework sees them as being the same.

这篇关于常规方法和异步方法(OnGet与OnGetAsync)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆