What is content negotiation in Web API?
Ask by ICSM Computer
Updated 13 Jun 2025
Content Negotiation (or conneg) in ASP.NET Web API is the process of determining the best format (media type) to return in the response, based on what the client can accept.
It's how the server chooses JSON, XML, or other formats based on:
AcceptheaderHow It Works
When a client sends a request to the API, it may include an
Acceptheader:ASP.NET Web API:
Acceptheader.🧪 Example
Request:
Response:
If the client had sent:
Then the server responds with:
Built-in Formatters in Web API
application/json,text/jsonapplication/xml,text/xmlYou can also create custom formatters for
CSV,YAML, etc.Server Configuration (Optional)
You can control or extend formatters in
WebApiConfig.cs:To remove XML support:
Use Cases
Force Format without Accept Header
You can use URL query strings or route extensions:
Requires custom logic or third-party packages (e.g.,
WebApiContrib.Formatting).Summary
Accept