Advertisement
Advanced ASP.NET Core, Content Negotiation & Output Formatters

Why is RespectBrowserAcceptHeader disabled by default in ASP.NET Core, and what problem does this default avoid?

Short answer

Browsers typically send an Accept header requesting HTML first (text/html) since that's their primary use case for regular web browsing, even when a developer manually navigates to an API URL expecting JSON -- with RespectBrowserAcceptHeader left at its default false, ASP.NET…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Browsers typically send an Accept header requesting HTML first (text/html) since that's their primary use case for regular web browsing, even when a developer manually navigates to an API URL expecting JSON -- with RespectBrowserAcceptHeader left at its default false, ASP.NET Core deliberately ignores this browser-supplied preference for API responses and returns the default format (JSON) instead, avoiding the confusing behavior of an API returning an unexpected format just because it was accessed via a browser address bar.
// Browser navigating directly to /api/products sends:
// Accept: text/html, application/xhtml+xml, ...

// With RespectBrowserAcceptHeader = false (default):
// API still returns JSON, ignoring the browser's HTML preference

// With RespectBrowserAcceptHeader = true:
// API might return 406 or attempt HTML formatting, which is rarely desired for a pure API
Real-world example
A developer debugging an API by pasting its URL directly into a browser address bar still sees a properly formatted JSON response instead of a confusing 406 error or garbled HTML attempt, thanks to this deliberate default.
Likely follow-ups
  • When would you actually want to enable RespectBrowserAcceptHeader?
  • How does this interact with an API that also serves some HTML content, like Swagger UI?
Related topics