Downloaded the WCF REST Template from this location.
The default response format is XML, which works great. However, when I try to get a JSON response, I still get XML.
This is my modified code -
[WebGet(UriTemplate = "",ResponseFormat = WebMessageFormat.Json)]
public List<SampleItem> GetCollection()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}
Note the ResponseFormat=WebMessageFormat.Json. That is the only change I did to that template.
What am I missing?
Thanks!
Figured out. automaticFormatSelectionEnabled
property for standardendpoint should be set to false
and defaultOutgoingReponseFormat should be set to Json
.
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="false"
defaultOutgoingResponseFormat ="Json" />
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
Changes to 2 attributes within the web.config will fix it:
automaticFormatSelectionEnabled=false
defaultOutgoingResponseFormat=Json
(edited: from "true")For me, setting the response format to JSON in the WebGet attribute doesn't work. Setting it in the body of the method does;
// This works
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
return jsonData;
// This doesn't work
`[WebGet(UriTemplate = "/conditions?term={term}", ResponseFormat = WebMessageFormat.Json)]`
Click -> reference links
"When automatic format selection is enabled, the infrastructure parses the Accept header of the request message and determines the most appropriate response format. If the Accept header does not specify a suitable response format, the infrastructure uses the Content-Type of the request message or the default response format of the operation."
EDIT: this link might get you moving ahead http://blogs.msdn.com/b/endpoint/archive/2010/11/01/wcf-webhttp-service-returns-http-415-unsupported-media-type.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With