不返回json格式,而是返回xml格式
本文关键字:返回 格式 xml json | 更新日期: 2023-09-27 18:11:50
我在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>
您需要设置ResponseFormat
为WebMessageFormat。Json
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "{id}/{name}/{age}/{sex}/{address}")]