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