What Is the Difference Between Regular and Async Methods (Onget vs Ongetasync)

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

1 Answer

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:

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

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest