What is the difference between regular and Async methods (OnGet vs OnGetAsync)

拟墨画扇 提交于 2019-12-11 02:28:16

问题


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 void OnGetAsync()
{
}

回答1:


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

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



来源:https://stackoverflow.com/questions/52895741/what-is-the-difference-between-regular-and-async-methods-onget-vs-ongetasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!