A binding source tells ASP.NET Core where to get a value for a handler parameter or binding-model property. Model binding turns request data into the .NET values a handler uses, so the handler does not have to read the URL or body itself. The available sources and the rules for choosing one differ between minimal APIs and Razor Pages. section 7.1 section 16.2
For a minimal API handler, the first matching rule wins:
- An explicit source attribute, such as [FromRoute], [FromQuery], or [FromBody], takes priority. section 7.8
- Otherwise, ASP.NET Core checks well-known types, a type's BindAsync method, simple-type route and query values, eligible query-string arrays, and registered services; it finally tries the JSON body. section 7.8
- For a simple type, a parameter name matching a route parameter selects the route value; otherwise, the query string is used. section 7.8
Razor Pages checks POSTed form values, route values, then query-string values by default, matching names without regard to case. It can fill separate properties of a complex model from different sources. By contrast, a minimal API binds a complex object from one source, and it does not automatically bind form fields as form values. In Razor Pages, use source attributes when a parameter must come from somewhere else: [FromHeader] selects a request header, while [FromBody] is required to bind a JSON body. section 16.2 section 16.2.3 section 16.5
A route value becomes a number
With `@page "{number}"`, a request containing `number=5` in the route can supply `OnGet(int number)`. The handler can calculate the square without extracting or converting the URL value itself. If Razor Pages finds no matching value, an `int` parameter receives its default value, 0; check whether that is acceptable for the handler. section 16.2.1
When a value comes from the wrong place, check the applicable source order and any explicit attributes. In a minimal API, a failed bind for a required parameter produces a 400 Bad Request response; it does not trigger a search of later sources. In Razor Pages, binding is followed by validation, whose result the page handler must check. section 7.8 section 16.5
