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()
{
}
There is no functional difference between naming your GET request handler OnGet
or OnGetAsync
. OnGetAsync
is just a naming convention for methods that contain asynchronous code that should be executed when a GET request is made. The body of the handler method is what determines whether your handler is asynchronous or not. You can omit the Async
suffix in the method name 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://learn.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.