问题
I have quite a few endpoints on my WCF REST Service. They all have the same body style, request format and response format.
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = xxx)]
Is there anyway to set those attributes in the web.config?
回答1:
You can set the default value for the body style and for the outgoing response format (not for the request format) in the <webHttp> endpoint behavior (see below). Notice that if you're in a service (which seems to be your case), that doesn't matter, since WCF REST endpoints can receive requests in both XML and JSON - the RequestFormat property is only used when it's being used within a client, to decide in which format to send the request.
<endpointBehaviors>
<behavior name="WebWithDefaults">
<webHttp defaultOutgoingResponseFormat="Json"
defaultBodyStyle="Bare" />
</behavior>
</endpointBehaviors>
来源:https://stackoverflow.com/questions/12902319/is-it-possible-to-set-the-request-response-format-in-web-config