ASP.NET Core MVC Interview Question #47
What is asynchronous action programming in ASP.NET Core MVC?
Controllers, Actions, Model Binding & Action Results Junior Beginner
Quick Interview Answer
An MVC action can be asynchronous and return Task<IActionResult> or another task-based result. Async actions are especially useful for database, HTTP, file, and other I/O operations because the request thread does not need to block while waiting for the I/O to complete.
Detailed Explanation
An MVC action can be asynchronous and return Task<IActionResult> or another task-based result. Async actions are especially useful for database, HTTP, file, and other I/O operations because the request thread does not need to block while waiting for the I/O to complete.
Async does not automatically make CPU-bound work faster. Avoid calling .Result or .Wait() on asynchronous operations in request code; use await throughout the call chain.
Code Example
public async Task<IActionResult> Details(
int id,
CancellationToken cancellationToken)
{
var product = await _service
.GetByIdAsync(id, cancellationToken);
if (product == null)
return NotFound();
return View(product);
}