Search code examples
jsonwcf-data-servicesatom-feedcontent-negotiation

How to disable content negotiation and always return JSON from WCF data service?


Let's say I have a northwind database and I use ADO.NET Entity Data Model which I automatically generate from the tables in database. Then I add a new WCF data service that inherits from DataService. When I start the web application, that runs the service I can request data like this: http://machine/Northwind.svc/Orders

This will return all orders from order table in atom/xml format. The problem is I do not want XML. I want JSON. I think I tried all kinds of settings (web.config) and attributes in my application, but I still get XML. No matter what. I can only get JSON, when I use fiddler and change the request header to accept JSON.

I do not like the concept of content negotiation. I want always to return data in JSON format. How can I achieve that?

Keep in mind that I did not create any model objects, they are automatically created based on database tables and relationships.


Solution

  • The behavior you criticize is defined by specification of OData protocol. OData defaults to Atom and client can control media type of the representation either by Accept HTTP header or by $format parameter in query string (but I'm not sure if WCF Data services already support this).