BindAsync lets a parameter type define custom model-binding logic for ASP.NET Core minimal APIs. The method receives HttpContext and returns the bound value, keeping request parsing inside the type. section 7.11 section 7.7
Use BindAsync when the built-in binding conventions do not match the request format:
- Implement either BindAsync(HttpContext) or BindAsync(HttpContext, ParameterInfo); the simpler signature is usually sufficient. section 7.7
- Read and parse request data through HttpContext, then return the constructed parameter value. section 7.7
- Return null when parsing fails for a required parameter so minimal APIs produce a 400 Bad Request response. section 7.7
- Remember that BindAsync is checked after explicit binding attributes and well-known types, but before TryParse, service injection, and JSON body binding. section 7.8
Parse a two-line size value
Map POST /sizes to a handler accepting SizeDetails. Define SizeDetails as a record with height and width, then implement its static async BindAsync method to read two lines from the request body and return null when either line is missing. A valid body containing two double values becomes a SizeDetails instance; an invalid required value produces a 400 response. section 7.7
public static ValueTask<T?> BindAsync(HttpContext context); public static ValueTask<T?> BindAsync( HttpContext context, ParameterInfo parameter);
Compare the two supported BindAsync signatures before choosing the parameter metadata overload. section 7.7
