0

我在我的 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; }
}

这是我的应用程序配置

    <?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>
4

1 回答 1

1

你需要设置ResponseFormatWebMessageFormat.Json

 [WebInvoke(Method = "GET", 
        ResponseFormat = WebMessageFormat.Json, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "{id}/{name}/{age}/{sex}/{address}")]
于 2013-05-03T01:20:21.520 回答