In ASP.NET Core MVC, why can only one action parameter be bound with [FromBody], and how do you accept a batch plus extra metadata?
answer
- the body is a stream
- one input formatter per action
- [ApiController]: InvalidOperationException up front
- envelope DTO or route/query/header
- [FromForm] may repeat
basics
~20 sAn input formatter reads the whole request body stream for one parameter, so a second [FromBody] parameter has nothing left to read. Wrap both payloads in one envelope type, or move scalar metadata to the route, query string or headers.
solid answer
~50 s`[FromBody]` hands the request body to an input formatter, which deserializes the entire stream into one object; once read, the stream is not rewound for another parameter. Under `[ApiController]`, MVC checks this when it builds the action and throws an `InvalidOperationException` saying only one parameter per action may be bound from body. In a plain controller there is no such check: the second parameter finds the stream exhausted, fails to bind, and `ModelState` records the formatter's error. The fix is a single envelope type such as `StockAdjustmentRequest { Batch, Reason }`, or moving small values out of the body: the warehouse id into the route, a dry-run flag into the query string, the correlation id into a header. Form binding is different: several `[FromForm]` parameters are fine, because the form is parsed once into `Request.Form` and each parameter reads keys from it.
go deeper
Remember that one action gets at most one [FromBody] parameter and that small values belong in the route, query string or headers.
Explain the stream and input formatter mechanics, the [ApiController] startup exception versus the silent runtime failure, and why forms differ.
Design request shapes so body, route, query and header each carry what they should, and catch inferred second body parameters in code review.
Set API design guidance that keeps request bodies to one cohesive document and puts identity and metadata elsewhere, so contracts stay stable.
## Why the body is single-use An HTTP request body arrives as a **forward-only stream**. In ASP.NET Core MVC, a parameter marked `[FromBody]` is bound by the body model binder, which picks an **input formatter** (by default `SystemTextJsonInputFormatter`) from the request's `Content-Type` and lets it deserialize the entire stream into one object. The default formatter does not rewind the stream afterwards, so nothing is left for a second body parameter. The ASP.NET Core documentation states the rule plainly: do not apply `[FromBody]` to more than one parameter per action. That is different from the other sources. Route values, query strings and headers are small, already-parsed collections that any number of parameters can read by key. ## What MVC does with two body parameters | Controller | When it is detected | What you see | |---|---|---| | With `[ApiController]` | when MVC builds the application model, before any request reaches the action | `InvalidOperationException`: the action "has more than one parameter that was specified or inferred as bound from request body. Only one parameter per action may be bound from body", followed by the parameter names | | Without `[ApiController]` | at request time | the first parameter binds; the second finds the body consumed, fails to bind, and `ModelState` carries an error for it | The `[ApiController]` check counts both explicit `[FromBody]` parameters and those it inferred, which is why adding a second complex parameter to an API controller action can break startup even without any attribute. ## The envelope fix The usual shape is one request type that groups everything the client sends in the body: ```csharp public record StockAdjustmentRequest( IReadOnlyList<StockLine> Lines, string Reason); [HttpPost("api/warehouses/{warehouseId:int}/stock-adjustments")] public IActionResult Post( int warehouseId, [FromBody] StockAdjustmentRequest request, [FromHeader(Name = "X-Correlation-Id")] string correlationId) => Accepted(); ``` Validation attributes on `StockLine` and on the envelope still run, because MVC validates the bound object graph recursively. ## Choosing where the extra values go 1. **Identity of the resource** (the warehouse) belongs in the **route**: it is part of the URL and shows in logs and OpenAPI paths. 2. **Options that tweak the operation** (`dryRun=true`) fit the **query string**. 3. **Cross-cutting metadata** (a correlation id) belongs in a **header**, bound with `[FromHeader]`. 4. **Everything that describes the change itself** (the lines and the reason) goes into the **one body object**. Moving values out of the body is not just a workaround: it also keeps the body schema focused on the data and lets routing constraints reject malformed ids before binding starts. ## Forms are not bound like bodies Several `[FromForm]` parameters on one action are legal. For a form post, ASP.NET Core parses the body once into `Request.Form` (an `IFormCollection`), and the form value provider serves each parameter by key from that parsed collection. A form post and a JSON body cannot share a request anyway: one request has one `Content-Type`. ## The empty-body corner `FromBodyAttribute` also exposes `EmptyBodyBehavior`: - `Default` defers to `MvcOptions.AllowEmptyInputInBodyModelBinding` (false unless you set it); under `[ApiController]`, an optional body parameter is switched to `Allow`. - `Allow` binds `null` when the request has no body. - `Disallow` always treats a missing body as an error, recording "A non-empty request body is required." in `ModelState`. ## What interviewers listen for - The stream explanation, not just "MVC forbids it". - That the startup exception is an `[ApiController]` behaviour, while plain controllers fail quietly at runtime. - A concrete redesign: one envelope, plus route, query and header for the rest. - That multiple `[FromForm]` parameters do not hit the same limit.
- Why can several [FromForm] parameters coexist on one action when [FromBody] cannot?A form body is parsed once into `Request.Form`, an `IFormCollection`, and the form value provider serves every parameter by key from that parsed collection. `[FromBody]` instead streams the raw body through an input formatter into a single object, so there is no parsed collection to share.
- Under [ApiController], how can adding one unattributed parameter break an action that already has [FromBody]?`[ApiController]` infers `[FromBody]` for complex types that are not registered services. A second complex parameter becomes a second inferred body parameter, and the convention throws `InvalidOperationException` when MVC builds the action. Marking it `[FromQuery]` or `[FromRoute]`, or folding it into the envelope, fixes it.
- What does EmptyBodyBehavior.Disallow change on a [FromBody] parameter?`Disallow` makes a missing body a binding error even when `MvcOptions.AllowEmptyInputInBodyModelBinding` is on or `[ApiController]` would treat an optional parameter as allowing it: MVC records "A non-empty request body is required." in `ModelState`. `Allow` binds `null` instead, and `Default` follows the option and the convention.
- Two [FromBody] parameters each receive their own copy of the request body.
- MVC splits a JSON body across several [FromBody] parameters by property name.
- A plain controller throws at startup for two [FromBody] parameters, just like [ApiController].
- Only one [FromForm] parameter is allowed per action, for the same reason as [FromBody].
- Wrapping payloads in an envelope type stops validation of the inner objects.