ASP.NET Core Web API Interview Question #8

What is the difference between route parameters and query-string parameters?

Web API & REST Fundamentals, Controllers, HTTP Methods & Status Codes Junior Beginner

Quick Interview Answer

Route parameters normally identify a particular resource or a hierarchical part of the resource path. Query-string parameters commonly represent optional filtering, searching, sorting, pagination, or other modifiers to a collection request.

Detailed Explanation

Route parameters normally identify a particular resource or a hierarchical part of the resource path. Query-string parameters commonly represent optional filtering, searching, sorting, pagination, or other modifiers to a collection request.

For example, /api/products/10 identifies product 10, while /api/products?category=phones&page=2 modifies the products collection being requested.

Code Example

[HttpGet("{id:int}")]
public IActionResult GetById(int id) => Ok(id);

[HttpGet]
public IActionResult Search(
    [FromQuery] string? category,
    [FromQuery] int page = 1)
{
    return Ok();
}