不以json格式返回,而是以xml格式返回

时间:2013-05-03 01:13:23

标签: c# json wcf

我的wcf服务库出现问题,因为我已将webMessegeFormat设置为JSON格式, 但它返回XML格式。我该如何解决这个问题?我错过了什么吗?

非常感谢那些愿意帮助的人:)

这是我的代码:

public class Service : iService
{
    [WebInvoke(Method="GET",
        RequestFormat = WebMessageFormat.Json,
        UriTemplate="{id}/{name}/{age}/{sex}/{address}")]
    public Response Transaction(string id, string name, string age, string sex, string address)
    {
        return new Response()
        {
            ID = id,
            Name = name,
            Age = age,
            Sex = sex,
            Address = address
        };
    }
}

public class Response
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
    public string Sex { get; set; }
    public string Address { get; set; }
}

这是我的app config

    <?xml version="1.0"?>
<configuration>

  <system.serviceModel>
    <services>
      <service name="WcfEServiceLibrary.Service">
        <endpoint address="http://phws13:8732/WcfServiceLibrary/" 
                  binding="webHttpBinding" 
                  contract="WcfServiceLibrary.iService">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

1 个答案:

答案 0 :(得分:1)

是否需要将ResponseFormat设置为 WebMessageFormat.Json

 [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Json, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "{id}/{name}/{age}/{sex}/{address}")]