liveBook · Manning

ReturnHttpNotAcceptable

As explained in Pro ASP.NET Core 7, Tenth Edition

ReturnHttpNotAcceptable is an MvcOptions setting that causes ASP.NET Core MVC to return HTTP 406 when no requested response format is supported.

ReturnHttpNotAcceptable is an MvcOptions setting for content negotiation. When enabled, ASP.NET Core MVC returns HTTP 406 instead of silently falling back to JSON when the requested response format is unsupported. section 20.4.2

Configure it alongside supported formatters:

  • Register the response formatters, such as JSON and XML. section 20.4.2
  • Set ReturnHttpNotAcceptable to true through the MVC options pattern. section 20.4.2
  • Test requests with an Accept header that names an unavailable media type. section 20.4.2

Rejecting an unsupported PNG request

An endpoint supporting JSON and XML receives Accept: img/png. With ReturnHttpNotAcceptable enabled, the request produces 406 Not Acceptable rather than a JSON response. section 20.4.2

Listing 20.17 Requesting a PNG response
Invoke-WebRequest http://localhost:5000/api/content/object -Headers 
@{Accept="img/png"} | select @{n='Content-Type';e={ $_.Headers.
"Content-Type" }}, Content

Inspect the unsupported-format request used to verify content-negotiation behavior. section 20.4.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains the option’s configuration and 406 behavior, but does not descend into formatter-selection internals.
Examples
Uses XML requests with fallbacks and an unsupported PNG request to demonstrate negotiated responses and rejection.
What it assumes
Assumes familiarity with MVC controllers and Accept headers while demonstrating formatter registration and options configuration.
What you can do after
Diagnose unsupported response-format requests by recognizing the intentional 406 result instead of an unexpected JSON fallback.
How it connects
Connects content negotiation with Accept headers, response formatters, JSON, XML, and HTTP 406 responses.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.