简体   繁体   中英

Does not return in a json format but instead in a xml format

I got issue in my wcf service library because I already set the webMessegeFormat to JSON format, but instead it returns the XML format. How can I fix this issue? Am i missing something?

Thank you so much in advance for those who will help :)

Here is my code:

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

Here is my 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>

do you need to set the ResponseFormat to WebMessageFormat.Json

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM