Calling the asynchronous method in the RazorPage model before creating the RazorPage View.

Volk Volk 551 Reputation points
2023-12-19T06:54:30.2+00:00

Hi!

How to correctly make an asynchronous method call in the RazorPage model (with asynchronous database accesses) before its view is built?

The call from the constructor is not correct, because there is no access to either User or other global data.

And calling an asynchronous method (or asynchronous requests like wait var a = await _db.Table...) in the public void OnGet() method, it does not have time to be executed and Null errors appear in the RazorPage View.

I make synchronous requests in the public void OnGet() method, but I would like asynchronous ones.

How to do it?

Thanks!

Developer technologies ASP.NET ASP.NET Core
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-12-19T16:47:30.4666667+00:00

    you use the async handler for the desired http verb. it is called before the processing of the razor template.

    public async Task OnGetAsync()
    {
    }
    public async Task OnPostAsync()
    {
    }
    

    note: this is all covered in the documentation.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.