如何从WCF服务返回Json? [英] How to return Json from WCF Service?

查看:188
本文介绍了如何从WCF服务返回Json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是启用Ajax模板的WCF服务的一段代码.我该怎么做才能使其返回JSon而不是XML? 谢谢.

I have the piece of code below of a template Ajax enabled WCF service. What can i do to make it return JSon instead of XML? thanks.

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;
    }
}

推荐答案

您是否尝试过:

[WebGet(ResponseFormat= WebMessageFormat.Json)]

这篇关于如何从WCF服务返回Json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆