skip to content

A Minimal API recipe search in ASP.NET Core 10 returns empty 400 responses in production and logs nothing; what does the binder do on failure, and how does ThrowOnBadRequest help?

level: seniorimportance: should knowfreq 38%

answer

  1. handler never runs on a bad parameter
  2. Debug-level log, filtered by default
  3. RouteHandlerOptions.ThrowOnBadRequest
  4. true only in Development
  5. BadHttpRequestException carries StatusCode

basics

~20 s

On a binding failure the Minimal API binder sets 400 (415 for a wrong content type), skips the handler and logs only at Debug. RouteHandlerOptions.ThrowOnBadRequest, true only in Development, throws BadHttpRequestException with the reason instead.

solid answer

~50 s

When a required parameter is missing, a value fails `TryParse`, or the JSON body cannot be read, the generated request delegate sets the status to 400 (415 when the content type is not JSON), does not call the handler and writes nothing to the body. It records the reason, such as "Failed to bind parameter ..." or "Required parameter ... was not provided", at **Debug** level in the `Microsoft.AspNetCore.Http.RequestDelegateFactory` category, which the template's `"Microsoft.AspNetCore": "Warning"` filter hides. `RouteHandlerOptions.ThrowOnBadRequest` makes the binder throw `BadHttpRequestException` with that message and a `StatusCode` instead; it defaults to `true` only when the environment is Development, so the developer exception page shows the reason locally while production sees a bare 400. To diagnose production, raise that log category to Debug briefly, or turn the option on and make sure whatever catches the exception keeps its status code.

code

json · 9 lines
json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.AspNetCore.Http.RequestDelegateFactory": "Debug"
    }
  }
}

go deeper

for a junior

Know that a bad or missing parameter makes a Minimal API answer 400 without running the handler.

for a middle

Explain the 400 and 415 cases, the Debug-level log entry, and that ThrowOnBadRequest is on only in Development.

for a senior

Diagnose silent production 400s by raising one log category, reproduce the exact input, and fix the contract with nullable or defaulted parameters.

for a principal

Set a policy for how binding failures are surfaced to clients and operators across services, balancing log volume against debuggability.

## What the binder does when binding fails Minimal API handlers are wrapped by a generated **request delegate** built by `RequestDelegateFactory`. Before the handler runs, that delegate fills every argument. If any argument fails, it records a failure, finishes checking the other parameters, and then **does not invoke the handler**: | Failure | Status | Log message (Debug) | |---|---|---| | required route, query or header value absent | 400 | Required parameter "int page" was not provided from query string. | | `TryParse` returned false (`?page=two`) | 400 | Failed to bind parameter "int page" from "two". | | `BindAsync` returned null for a non-nullable parameter | 400 | Required parameter ... was not provided from ...BindAsync(...). | | JSON body malformed | 400 | Failed to read parameter ... from the request body as JSON. | | inferred body required but empty | 400 | Implicit body inferred for parameter ... but no body was provided. | | body content type is not JSON | 415 | Expected a supported JSON media type but got ... | By default the response has **no body**, and the log entries are written at **`LogLevel.Debug`** under the category `Microsoft.AspNetCore.Http.RequestDelegateFactory`, with event names such as `ParameterBindingFailed` and `RequiredParameterNotProvided`. The `webapi` template's `appsettings.json` sets `"Microsoft.AspNetCore": "Warning"`, so in a fresh project those lines never reach a sink. The result is the support ticket in the question: clients see 400 with an empty body, and the server shows nothing. ## RouteHandlerOptions.ThrowOnBadRequest `RouteHandlerOptions` (namespace `Microsoft.AspNetCore.Routing`) has one property, `ThrowOnBadRequest`. When it is true, the same failure points **throw `BadHttpRequestException`** after logging. The exception carries the message from the table above and a `StatusCode` of 400 or 415. Its default comes from the environment: - In **Development**, an internal options configurator sets it to `true`. - Everywhere else it stays `false`, the property's default. In Development, the developer exception page catches the exception, keeps the status code the exception carries, and shows the message. That is why a binding mistake is obvious on a laptop and invisible in production. ## Diagnosing the production 400s Work from cheapest to most invasive: 1. **Raise one category to Debug** for a short window: `"Microsoft.AspNetCore.Http.RequestDelegateFactory": "Debug"` in configuration. The next failing request logs the exact parameter and raw value. 2. **Reproduce with the raw request.** Typical causes in a recipe search are a client sending `pageSize=` with no value, `page=2.0`, a culture-formatted number (TryParse uses the invariant culture), or `Content-Type: text/plain` on a POST body. 3. **Turn the option on deliberately**: `builder.Services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);`. If nothing catches the exception, Kestrel answers with its `StatusCode` and logs it as an application error. Behind the exception handler middleware, which answers 500 for any exception by default, you must map `BadHttpRequestException.StatusCode` yourself, or every client typo becomes a server error. 4. **Give the empty 400 a body.** Writing a ProblemDetails response for bodiless error statuses is a separate concern, handled by the problem-details services and the status code pages middleware. ## Why the default is so quiet The silence is deliberate. A binding failure is the **client's** mistake, and public APIs receive a steady stream of malformed requests from scanners and outdated clients. Logging each one at Warning or throwing an exception per request would bury real server errors and inflate error-rate metrics. ASP.NET Core therefore answers the cheapest correct status, records the reason at a level you opt into, and saves the exception for Development, where a developer is watching the screen. The cost is the debugging session in this question, which is why knowing the category name matters. ## Fixing the contract rather than the symptom - Make genuinely optional inputs **nullable or defaulted** (`int page = 1`, `string? q`), so a missing value is not a failure. - Treat parse failures as the client's fault, and document the expected format; a nullable type still returns 400 for an unparsable value. - Keep `ThrowOnBadRequest` off in production unless you have decided how the exception is logged and which status it maps to; otherwise every bad query string becomes an exception in your logs, and possibly a 500. ## What interviewers listen for - That the handler **never runs** on a binding failure and the 400 is **bodiless** by default. - The **Debug** level and the category to raise. - That `ThrowOnBadRequest` **defaults to true only in Development**, and what it changes. - A fix that addresses the client contract, not just the logging.

  • With ThrowOnBadRequest on in production, what status does the client get?
    It depends on what catches the exception. `BadHttpRequestException` carries a `StatusCode` (400, or 415 for a wrong content type). If nothing catches it, Kestrel answers with that status and logs the exception; the developer exception page also preserves it. The exception handler middleware, however, answers 500 for any exception by default, so behind `UseExceptionHandler` you must map the exception's `StatusCode` yourself.
  • Does a binding failure still run the endpoint's filters?
    When endpoint filters are registered, the generated delegate records the failure and still enters the filter pipeline, but the innermost step sees a status of 400 or more and returns an empty result instead of calling the handler. So a filter can observe or log the failed request, yet the handler body never executes with half-bound arguments.

A restaurant host who checks reservations at the door: when a guest's details do not match, the host quietly turns them away and jots a note in a notebook nobody reads. ThrowOnBadRequest is the setting where the host calls the manager over to explain each refusal; it is on during training shifts and off on a normal night.

  • A binding failure calls the handler with default values for the bad parameters.
  • ThrowOnBadRequest defaults to true in every environment.
  • Binding failures are logged as warnings, so the default template shows them.
  • With ThrowOnBadRequest on, the handler receives the BadHttpRequestException to handle itself.
  • A nullable parameter turns an unparsable value into null instead of a 400.