Asynchronous actions

ASP.NET Core supports asynchronous actions that are implemented with the async-await pattern. If the method action return type is Task or Task<T>, ASP.NET Core will await the Task, and once completed, the result will be sent back to the client.

For example, here is an asynchronous version of the AddNewProduct action that adds a short delay (for demonstration purposes) and awaits it:

[HttpPost("")]public async Task<ActionResult<NewProductDTO>> AddNewProduct([FromBody] NewProductDTO newProduct){    await Task.Delay(1000);    if (!ModelState.IsValid)    {        return new BadRequestResult();//(ModelState);    }    return new ObjectResult(newProduct);}

Get Hands-On Full-Stack Web Development with ASP.NET Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.