Explore Library
Code QuizAdvanced

Route Parameter Binding Mismatch

A Web API GET action fails to bind its id because the route template and parameter name disagree.

Codecsharp
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IProductRepo _repo;
    public ProductsController(IProductRepo repo) => _repo = repo;

    [HttpGet("{id}")]
    public IActionResult GetProduct(int productId)
    {
        var product = _repo.Find(productId);
        if (product == null) return NotFound();
        return Ok(product);
    }
}

GET api/products/5 always returns the product with id 0. What is the bug?