ASP.NET Core MVC Interview Question #34

What is the difference between [FromRoute], [FromQuery], and [FromForm]?

Controllers, Actions, Model Binding & Action Results Junior Beginner

Quick Interview Answer

FromRoute tells MVC to obtain a value from route data, FromQuery obtains it from the query string, and FromForm obtains it from submitted form fields. These attributes make the source explicit and can remove ambiguity.

Detailed Explanation

FromRoute tells MVC to obtain a value from route data, FromQuery obtains it from the query string, and FromForm obtains it from submitted form fields. These attributes make the source explicit and can remove ambiguity.

For conventional MVC forms, explicit attributes are not always necessary because model binding conventions often identify the source automatically.

Code Example

[HttpPost("products/{id:int}")]
public IActionResult Update(
    [FromRoute] int id,
    [FromQuery] bool returnToList,
    [FromForm] ProductEditViewModel model)
{
    return RedirectToAction(nameof(Index));
}