Reputation: 1584
I can't seem to succeed with getting JSON out of a WCF service even if I tag the method with the attributes:
[WebGet(UriTemplate = "Product/{productIdString}",
ResponseFormat = WebMessageFormat.Json)]
OR
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
I'm always getting XML, whether I return it as a DataSet
or a List<>
.
The only way that worked was to manually returning JSON as a string but it was also encapsulated in XML.
Any clue?
Upvotes: 0
Views: 749
Reputation: 46
try something like this:
[WebInvoke(Method = "GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "products")]
public IList<JxProduct> GetProductList()
{
List<JxProduct> products = new List<JxProduct>();
products.Add(new JxProduct { Description = "Tire", Id = 1, Price = 39.99});
products.Add(new JxProduct { Description = "Tube", Id = 2, Price = 4.99 });
products.Add(new JxProduct { Description = "Patch", Id = 3, Price = 3.99});
return products;
}
You can also review the following post which goes into more details about web.config settings. How do I return clean JSON from a WCF Service?
Upvotes: 2
Reputation: 87308
[WebGet]
(and [WebInvoke]
) attributes are only recognized by the WebHttpBehavior
(<webHttp/>
if you're using config). Make sure that the endpoint you're hitting has that behavior set.
Upvotes: 0