I have a WCF 4.0 REST service. If I enable automaticFormatSelectionEnabled in the web.config file then the service will correctly choose between serializing as XML or JSON based on the HTTP “Accept” header.
However, when I issue the GET command with a browser, the response body comes back formatted as XML, but the HTTP content-type header is “text/html”. This causes the browser to not realize the response is XML and try to render it as html (which of course doesn’t work well). This makes testing my GET methods in a browser more difficult.
If I disable automaticFormatSelectionEnabled then everything works as expected (response body contains XML and the HTTP content-type is “application/xml”), however, I’d like to be able to automatically switch to JSON if requested.
Is there some way to make the content-type come back correctly when requesting through the browser?
I ran into the same problem, here is a work around. Essentially what you need to do is create a behaviourextension that will change the content type when sending the response.
Your web.config would need to include something like:
Then you will need to create a class that inherits from BehaviorExtensionElement
And then finally a class that implements IDispatchMessageInspector and IServiceBehavior which does the hard work of changing the content type:
Should work as expected now!