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
