我正在编写一个有很多方法的web服务。它们的设置都类似于以下内容:
[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 / RequestFormat / ResponseFormat。现在,我知道我能做到:
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" />
</behavior>
</endpointBehaviors>但是似乎没有用于RequestFormat的属性。如何将默认RequestFormat设置为JSON?
发布于 2012-08-03 21:19:43
请求类型为automatically interpreted by WCF,您不需要为您的服务操作指定默认RequestFormat。
如果您尝试强制使用受支持的请求格式,请参阅this related SO post on enforcing request content types。
注意:为WebGet操作分配RequestFormat是没有意义的。根据定义,WebGet不能包含JSON格式所在的Body。这里一个更好的例子是WebInvoke。
发布于 2015-07-17 17:22:06
在web.config文件的webHttp元素中将automaticFormatSelectionEnabled属性设置为true
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp automaticFormatSelectionEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>例如:可以在接收端设置Accept:application/json,得到JSON结果。
邮递员屏幕

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

https://msdn.microsoft.com/en-us/library/ee476510(v=vs.110).aspx
https://stackoverflow.com/questions/11786281
复制相似问题