NeuroAgent

Complete Guide: Configure ASP.NET Web API JSON Responses

Learn multiple methods to configure ASP.NET Web API to return JSON instead of XML in Chrome. Discover server-side configuration options, client-side header manipulation, and browser testing solutions for reliable JSON responses.

How can I configure ASP.NET Web API to return JSON instead of XML when viewed in Chrome? I’m currently seeing XML responses in my browser and would like to switch to JSON format. Is this controlled by request headers, and if so, how can I modify them to request JSON?

NeuroAgent

ASP.NET Web API returns XML by default when accessed through Chrome because browsers typically send Accept headers that prioritize XML. You can configure your API to return JSON instead by either modifying the server-side configuration to make JSON the default format or by properly setting the Accept request header to application/json in your browser requests. The most reliable approach involves configuring your Web API formatters to prioritize JSON responses.

Contents


Understanding the Problem

When you access an ASP.NET Web API endpoint directly in Chrome, you typically see XML responses instead of JSON. This happens because Web API looks for the headers of the upcoming request to choose the returning data type. Specifically, if you set Accept:application/json it will automatically set the returning type to JSON.

The issue stems from how browsers handle Accept headers. Unlike dedicated API clients, web browsers tend to supply Accept headers that include a wide array of formats, including wildcards. By default, when the framework detects that the request is coming from a browser, it will ignore the Accept header and instead return the content in the application’s configured default format.

Key Insight: Chrome and other browsers often send Accept headers like text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 which prioritize XML over JSON for API responses.


Server-Side Configuration Options

Remove XML Formatter Completely

The most straightforward approach is to completely remove the XML formatter from your Web API configuration, forcing it to return JSON by default:

csharp
public static void Register(HttpConfiguration config)
{
    config.Formatters.Clear();
    config.Formatters.Add(new JsonMediaTypeFormatter());
}

This approach completely removes the XML formatter, forcing ASP.NET Web API to return JSON by default. However, this solution is quite restrictive as it eliminates the ability to return XML when needed.

Configure Default Content Negotiator

You can configure the DefaultContentNegotiator to return a 406 status code when no matching representation is found:

csharp
config.Services.Replace(typeof(IContentNegotiator), new DefaultContentNegotiator());

According to the research, you can configure the DefaultContentNegotiator to instead of returning a default format then return a 406 None Accepted status code. This indicates to the client that no matching representation could be found.

Set JSON as Default Formatter

In your WebApiConfig.cs file, you can configure JSON to be the default formatter:

csharp
config.Formatters.JsonFormatter.SerializerSettings.Formatting = 
    Newtonsoft.Json.Formatting.Indented;
config.Formatters.Remove(config.Formatters.XmlFormatter);

As one source explains, if you do this in the WebApiConfig you will get JSON by default, but it will still allow you to return XML if needed.


Client-Side Header Manipulation

Using Browser Developer Tools

Chrome Developer Tools allow you to modify request headers before sending them:

  1. Open Chrome DevTools (F12)
  2. Go to Network tab
  3. Find your API request
  4. Right-click and select “Copy” → “Copy as cURL”
  5. Edit the copied command to include Accept: application/json

Browser Extension Solutions

You can install browser extensions that allow you to modify request headers:

  • ModHeader - Add custom headers to your requests
  • Requestly - Rules to modify requests and responses
  • Postman - Chrome extension for API testing with header control

As mentioned in the research, you can get the Postman extension which allows you to set the request content type.

Using curl or Command Line Tools

For testing purposes, you can use curl with proper headers:

bash
curl -H "Accept: application/json" "https://your-api-endpoint"

Browser Testing Solutions

Chrome about:config Method

For Firefox users, you can modify the default Accept header:

  1. Type about:config in the address bar
  2. Search for accept.default
  3. Change network.http.accept.default to text/html,application/xhtml+xml,application/json;q=0.9,application/xml;q=0.8,*/*;q=0.7

However, this Firefox-specific approach won’t work in Chrome. For Chrome, you’ll need to use extensions or testing tools.

Fiddler or http-repl Tools

As Microsoft documentation explains, tools such as Fiddler or http-repl can set the Accept request header to specify the return format. When the Accept header contains a type the server supports, that type is returned.

Dedicated API Testing Tools

For reliable API testing, consider using:

  • Postman - Full-featured API testing tool
  • Insomnia - API client with header management
  • Advanced REST Client - Chrome extension for API testing

Best Practices

Production Configuration

For production environments, it’s recommended to:

  1. Keep both formatters - Allow flexibility for both JSON and XML consumers
  2. Set proper content negotiation - Let clients specify their preferred format
  3. Use versioning - Support multiple formats while maintaining API versioning

Development Testing

During development:

  1. Use proper API testing tools - Avoid relying on browser direct access
  2. Configure default formatter - Set JSON as default for development convenience
  3. Document API requirements - Clearly specify expected Accept headers

Security Considerations

Be aware that:

  1. Header manipulation can be bypassed - Server configuration should be authoritative
  2. Content sniffing - Some browsers may override content-type headers
  3. CORS considerations - Ensure your API properly handles cross-origin requests

Sources

  1. How do I get ASP.NET Web API to return JSON instead of XML using Chrome? - Stack Overflow
  2. How To Get ASP.NET Web API to Return JSON Instead of XML in a Browser - travis.io
  3. ASP.NET web api returning XML instead of JSON - Stack Overflow
  4. Format response data in ASP.NET Core Web API | Microsoft Learn
  5. ASP.NET WebApi returns XML or json based on the request header - Stack Overflow
  6. WebAPI not returning XML even with correct Accepts header - Stack Overflow
  7. JSON and XML Serialization in ASP.NET Web API - ASP.NET 4.x | Microsoft Learn
  8. Web API returns the JSON but not Xml - Stack Overflow

Conclusion

To configure ASP.NET Web API to return JSON instead of XML in Chrome, you have several effective options. The most reliable server-side approach is to modify your WebApiConfig.cs to set JSON as the default formatter or remove the XML formatter entirely. For client-side solutions, you can use browser extensions like ModHeader or Postman to set the Accept: application/json header in your requests. Remember that browser direct access to APIs isn’t ideal for testing - consider using dedicated API testing tools for more reliable results. For production environments, maintaining both formatters while allowing content negotiation through proper Accept headers provides the best flexibility for different API consumers.