Someone asked me recently a quick question on how to set up a RESTful service to return JSON instead of XML. Th person already had a RESTful service they had created in WCF that returned XML, so most of the hard work was done already. This was a quick answer (nice to have a quick question/answer once in a while!)  since the WebGetAttribute class expose a property called ResponseFormat which accepts a WebMessageFormat enumeration value. The values are Xml and Json. So to configure a method to return a Product class serialized as JSON, simply do this:


[WebGet(UriTemplate = "Product/{productIdString}", 
    ResponseFormat = WebMessageFormat.Json)]
public Product FindProduct(string productIdString)
    int productId = int.Parse(productIdString);
    return GetProduct(productId);


<OperationContract, WebGet(UriTemplate := "Product/{productIdString}", _
    ResponseFormat := WebMessageFormat.Json)> _
Public Function FindProduct(ByVal productIdString As String) As Product
    Dim productId As Integer = Integer.Parse(productIdString)
    Return GetProduct(productId)
End Function

To Flip this to return XML instead, modify the example to set the ResponseFormat to WebMessageFormat.Xml

And that’s it!

DotNetKicks Image