liveBook · Manning

BadHttpRequestException

As explained in ASP.NET Core in Action, 3e

BadHttpRequestException is an ASP.NET Core exception indicating that an HTTP request is malformed or contains values that cannot be bound or interpreted.

BadHttpRequestException indicates that a minimal API could not create an endpoint argument from the HTTP request. ASP.NET Core returns a 400 Bad Request response instead of calling the handler. section 7.11 section 7.2

Expect this failure when model binding cannot produce every handler parameter, including when:

  • A simple value such as an int cannot be parsed from a route value, query string value, or header. section 7.11 section 7.2
  • A required value is missing from the request. section 7.11
  • A custom type's TryParse method returns false. section 7.11
  • A request attempts unsupported or unexpected body binding, such as binding a GET body or accessing form values through model binding. section 7.11

A strongly typed route value

An endpoint can accept a ProductId instead of a raw string when the type exposes TryParse. For /product/p123, successful parsing creates the handler argument; an invalid value produces a 400 response. section 7.2

Figure 7.3 The routing middleware matches the incoming URL to the endpoint. The endpoint middleware attempts to bind the route parameter id to the endpoint parameter. The endpoint parameter type ProductId implements TryParse. If parsing is successful, the parsed parameter is used to call the endpoint handler. If parsing fails, the endpoint middleware returns a 400 Bad Request response.
CH07_F03_Lock3

Follow the request path from route matching through TryParse and handler invocation, including the failure path that returns 400 Bad Request. section 7.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains binding failures, supported sources, custom TryParse behavior, and response status, but not exception customization.
Examples
Uses incompatible integer input, missing parameters, and a ProductId TryParse implementation as concrete binding examples.
What it assumes
Assumes basic endpoint handlers and HTTP requests while explaining model binding and simple types from their request sources.
What you can do after
Diagnose/debug with it by tracing failed parameter binding and distinguishing malformed input from handler execution.
How it connects
Connects model binding with minimal APIs, route values, query strings, headers, JSON bodies, and TryParse.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.