我在WCF Rest服务的Code下面使用JSON格式
[OperationContract]
[WebGet(UriTemplate = "/GetOrderList?request={request}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
IEnumerable<Order> GetOrderList(Request request);
Run Code Online (Sandbox Code Playgroud)
我希望此方法也返回XML类型.我还需要一种方法吗?我希望在不重复XML代码的情况下以相同的方法执行此操作.我正在使用WCF 3.5.我不能改变我的版本.
Sat*_*chi 18
我遇到了同样的问题.我们通过为XML创建两个端点而为JSON创建另一个端点来提供解决方案.
确保从服务界面中删除所有属性.不要指定RequestFormat或ResponseFormat来控制XML或JSON.让它由端点控制.
服务Web.Config更改.
<endpoint address="XML" binding="webHttpBinding" bindingConfiguration="webHttpBindingXML" contract="xxxxxx.Ixxxxxxx" behaviorConfiguration="RestXMLEndpointBehavior"/>
<endpoint address="JSON" binding="webHttpBinding" bindingConfiguration="webHttpBindingJSON" contract="xxxxxxxx.Ixxxxxxxxx" behaviorConfiguration="RestJSONEndpointBehavior"/>
<endpointBehaviors>
<behavior name="RestJSONEndpointBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
</behavior>
<behavior name="RestXMLEndpointBehavior">
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Xml"/>
</behavior>
</endpointBehaviors>
<webHttpBinding>
<binding name="webHttpBindingXML"/>
<binding name="webHttpBindingJSON"/>
</webHttpBinding>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
小智 6
你甚至不需要在这里指定返回类型,我们在端点行为中有一个名为automaticFormatSelectionEnabled的属性用于WebGet,如下所示.当您从客户端发出休息呼叫请求时,可以将类型指定为 WebClient.Headers ["Content-type"] ="application/json"; 或WebClient.Headers ["Content-type"] ="application/xml"; ,服务将检测类型并返回您想要的正确格式..
<endpointBehaviors>
<behavior name="RestServiceEndPointBehavior">
<webHttp automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13372 次 |
| 最近记录: |