---
title: "What is content negotiation in Web API?"  
description: "What is content negotiation in Web API?"  
author: "Ravi Vishwakarma"  
published: 2025-06-13  
updated: 2025-06-13  
canonical: https://www.mindstick.com/interview/34240/what-is-content-negotiation-in-web-api  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What is content negotiation in Web API?

**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:

- The client's `Accept` **header**
- Available **formatters** on the server

## How It Works

When a client sends a request to the API, it may include an `Accept` header:

```plaintext
GET /api/products
Accept: application/json
```

ASP.NET Web API:

- Looks at the `Accept` header.
- Matches it with the available media type formatters (e.g., JSON, XML).
- Serializes the response in the best matching format.

## 🧪 Example

### Request:

```plaintext
GET /api/products
Accept: application/xml
```

### Response:

```xml
<Product><Id>1</Id><Name>Phone</Name></Product>
```

If the client had sent:

```plaintext
Accept: application/json
```

Then the server responds with:

```plaintext
{ "Id": 1, "Name": "Phone" }
```

## Built-in Formatters in Web API

| Formatter | Media Type(s) |
| --- | --- |
| **JsonMediaTypeFormatter** | `application/json`, `text/json` |
| **XmlMediaTypeFormatter** | `application/xml`, `text/xml` |

You can also create **custom formatters** for `CSV`, `YAML`, etc.

## Server Configuration (Optional)

You can control or extend formatters in `WebApiConfig.cs`:

```plaintext
config.Formatters.JsonFormatter.SupportedMediaTypes
      .Add(new MediaTypeHeaderValue("text/html"));
```

To remove XML support:

```plaintext
config.Formatters.Remove(config.Formatters.XmlFormatter);
```

## Use Cases

| Use Case | Benefit |
| --- | --- |
| Mobile apps prefer JSON | Lightweight, faster parsing |
| Legacy systems require XML | Compatibility |
| Browser default is HTML | Can force JSON via custom formatter |

## Force Format without Accept Header

You can use **URL query strings** or **route extensions**:

```plaintext
/api/products?format=json
/api/products.json
```

Requires custom logic or third-party packages (e.g., `WebApiContrib.Formatting`).

## Summary

| Feature | Description |
| --- | --- |
| **Definition** | Process of selecting response format based on client preference |
| **Header Used** | `Accept` |
| **Formats Supported** | JSON, XML (by default) |
| **Customizable** | Yes, you can add/remove/override formatters |

## Answers

### Answer by Ravi Vishwakarma

**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:

- The client's `Accept` **header**
- Available **formatters** on the server

## How It Works

When a client sends a request to the API, it may include an `Accept` header:

```plaintext
GET /api/products
Accept: application/json
```

ASP.NET Web API:

- Looks at the `Accept` header.
- Matches it with the available media type formatters (e.g., JSON, XML).
- Serializes the response in the best matching format.

## 🧪 Example

### Request:

```plaintext
GET /api/products
Accept: application/xml
```

### Response:

```xml
<Product><Id>1</Id><Name>Phone</Name></Product>
```

If the client had sent:

```plaintext
Accept: application/json
```

Then the server responds with:

```plaintext
{ "Id": 1, "Name": "Phone" }
```

## Built-in Formatters in Web API

| Formatter | Media Type(s) |
| --- | --- |
| **JsonMediaTypeFormatter** | `application/json`, `text/json` |
| **XmlMediaTypeFormatter** | `application/xml`, `text/xml` |

You can also create **custom formatters** for `CSV`, `YAML`, etc.

## Server Configuration (Optional)

You can control or extend formatters in `WebApiConfig.cs`:

```plaintext
config.Formatters.JsonFormatter.SupportedMediaTypes
      .Add(new MediaTypeHeaderValue("text/html"));
```

To remove XML support:

```plaintext
config.Formatters.Remove(config.Formatters.XmlFormatter);
```

## Use Cases

| Use Case | Benefit |
| --- | --- |
| Mobile apps prefer JSON | Lightweight, faster parsing |
| Legacy systems require XML | Compatibility |
| Browser default is HTML | Can force JSON via custom formatter |

## Force Format without Accept Header

You can use **URL query strings** or **route extensions**:

```plaintext
/api/products?format=json
/api/products.json
```

Requires custom logic or third-party packages (e.g., `WebApiContrib.Formatting`).

## Summary

| Feature | Description |
| --- | --- |
| **Definition** | Process of selecting response format based on client preference |
| **Header Used** | `Accept` |
| **Formats Supported** | JSON, XML (by default) |
| **Customizable** | Yes, you can add/remove/override formatters |


---

Original Source: https://www.mindstick.com/interview/34240/what-is-content-negotiation-in-web-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
