LostyBoj
LostyBoj

Reputation: 53

Why async and await in Razor Pages NET CORE

I am havin a course for .NET Core, and they learn us to use

public async Task OnGet()
{
}

and

public async Task<IActionResult> OnPost()
{
    await database.SMTH.FindAsync()
}

why they are not using simple

public IActionResult OnGet();

and

public IActionResult OnPost()
{
}

This is the first time I have met this using async and await in .NET Core Razor Pages. And I dont know why they are doing it this way. (I know what async methods are.) Thank you.

Upvotes: 1

Views: 2092

Answers (1)

Mike Brind
Mike Brind

Reputation: 30110

The methods you posted are handler methods that execute when pages are loaded, based on the HTTP verb that was used to make the request. If you have asynchronous code to run e.g.to get data from a database, when your page loads, you need to place that in an asynchronous handler method - one that has the async operator and returns a Task or Task<T>.

Upvotes: 2

Related Questions