8

我正在编写一个有很多方法的网络服务。它们的设置都类似于以下内容:

[OperationContract]
    [WebInvoke(
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "x/y/z")]
    void someMethod(int x, int y, int z);

我要做的只是在 web.config 文件中设置默认值BodyStyle// all RequestFormatResponseFormat现在,我知道我可以做到这一点:

  <endpointBehaviors>
    <behavior name="webHttpBehavior">
      <webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

但似乎没有 RequestFormat 的属性。如何将默认设置RequestFormat为 JSON?

4

2 回答 2

5

请求类型由 WCF 自动解释,您无需RequestFormat为服务操作指定默认值。

如果您尝试强制执行支持的请求格式,请参阅有关强制请求内容类型的相关 SO 帖子

注意:RequestFormat为操作分配 a 是没有意义的WebGet。根据定义,aWebGet不能包含BodyJSON 格式存在的地方。一个更好的例子是WebInvoke.

于 2012-08-03T13:19:42.663 回答
1

automaticFormatSelectionEnabled属性设置为web.config 文件true中的webHttp元素

<behaviors>
   <endpointBehaviors>
      <behavior>
         <webHttp automaticFormatSelectionEnabled="true" />
      </behavior>
   </endpointBehaviors>
</behaviors>


eg:你可以Accept:application/json在接收端设置并获取JSON结果。

邮递员屏风

json响应

==================================================== ===================

xml 响应


https://msdn.microsoft.com/en-us/library/ee476510(v=vs.110).aspx

于 2015-07-17T09:22:06.127 回答