Using $format=json out of the box against .NET 4 WCF Data Service will not work even if the OData Spec says it is supported. I donβt know exactly why Microsoft does not directly support it. But in this situation, there are two workarounds: the person feels a little hacked, and the other makes sense.
First, a solution that is a bit hacked is to create an HttpHandler that intercepts your request, reads the querystring $format=json parameter, and then adds the accepts header to your request (when removing the offensive $format=json parameter). This is described in this blog post .
The second solution, which sounds a little better, is to decorate your data service with the [JSONPSupportBehavior] attribute. This makes a little more sense and is a little easier to implement (since you don't need to create an HttpHandler). Here are some useful links:
- Blog post describing how to use it.
- Link to download the source code for the
[JSONPSupportBehavior] attribute (yes, you have to build it - I did not find the compiled download).
I like the attribute approach, I'm just sorry it wasn't loading from CodePlex ... it just doesn't sound that supported. But this is just my opinion.
Honestly, if you have control, the best approach is to simply add the accepts header to your application/json request and your service will automatically return formatted JSON results.
Hope this helps.
source share