如何从WCF服务返回Json?

Zin*_*noo 9 wcf json

我在下面的代码片段中启用了一个支持Ajax的WCF服务模板.我该怎么做才能让它返回JSon而不是XML?谢谢.

using System; 
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;

[ServiceContract(Namespace = "WCFServiceEight")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CostService
{
    // Add [WebGet] attribute to use HTTP GET
    [OperationContract]
    [WebGet]
    public double CostOfSandwiches(int quantity)
    {
        return 1.25 * quantity;
    }
}
Run Code Online (Sandbox Code Playgroud)

tom*_*asr 7

你有没有尝试过:

[WebGet(ResponseFormat= WebMessageFormat.Json)]
Run Code Online (Sandbox Code Playgroud)