C# 从WCF获取XML格式的输出,但不获取JSON输出
当我用C# 从WCF获取XML格式的输出,但不获取JSON输出,c#,wcf,rest,C#,Wcf,Rest,当我用WebMessageFormat.Xml调用方法XMLData时,我得到如下响应: 当我使用WebMessageFormat.Json调用方法XMLData时,我得到如下响应: WCF代码: namespace RestService { [ServiceContract] public interface IRestServiceImpl { [OperationContract] [WebGet(ResponseFormat
WebMessageFormat.Xml
调用方法XMLData
时,我得到如下响应:
当我使用WebMessageFormat.Json
调用方法XMLData
时,我得到如下响应:
WCF代码:
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string XMLData(string id);
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
string JSONData();
}
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string XMLData(string id)
{
return "You requested product " + id;
}
public string JSONData()
{
return "You requested product ";
}
#endregion
}
}
配置文件:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl">
<endpoint name="jsonEP"
address=""
binding="webHttpBinding"
behaviorConfiguration="json"
contract="RestService.IRestServiceImpl"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="json">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我的代码怎么了?JSON是无序的键:值对集合的序列化格式,键和值之间用“:”字符分隔,逗号分隔,通常用大括号(对象)、括号(数组)或引号(字符串)括起来 虽然您的响应是JSON格式的,但它也是字符串格式的纯文本!没有要序列化的对象/数组,也没有用于响应的键/值对,这就是firebug在网络选项卡中不显示任何JSON预览的原因 尝试在REST服务中返回一些复杂对象,您将在Firebug调试器中看到JSON响应预览:
public class RestServiceImpl : IRestServiceImpl
{
public JSONResponse JSONData(string id)
{
return new JSONResponse { Response = "You requested product " + id };
}
}
public class JSONResponse
{
public string Response { get; set; }
}
JSON是键:值对无序集合的序列化格式,键和值之间用“:”字符分隔,逗号分隔,通常用大括号(对象)、括号(数组)或引号(字符串)括起来 虽然您的响应是JSON格式的,但它也是字符串格式的纯文本!没有要序列化的对象/数组,也没有用于响应的键/值对,这就是firebug在网络选项卡中不显示任何JSON预览的原因 尝试在REST服务中返回一些复杂对象,您将在Firebug调试器中看到JSON响应预览:
public class RestServiceImpl : IRestServiceImpl
{
public JSONResponse JSONData(string id)
{
return new JSONResponse { Response = "You requested product " + id };
}
}
public class JSONResponse
{
public string Response { get; set; }
}