从下载的WCF REST模板这个位置.
默认的响应格式是XML,效果很好.但是,当我尝试获取JSON响应时,我仍然获得XML.
这是我修改过的代码 -
[WebGet(UriTemplate = "",ResponseFormat = WebMessageFormat.Json)]
public List<SampleItem> GetCollection()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances
return new List<SampleItem>() { new SampleItem() { Id = 1, StringValue = "Hello" } };
}
Run Code Online (Sandbox Code Playgroud)
请注意ResponseFormat = WebMessageFormat.Json.这是我对该模板所做的唯一改变.
我错过了什么?
谢谢!
Rya*_*yan 56
想通了.automaticFormatSelectionEnabled应将standardendpoint的属性设置为,false并将defaultOutgoingReponseFormat设置为Json.
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="false"
defaultOutgoingResponseFormat ="Json" />
Run Code Online (Sandbox Code Playgroud)
小智 6
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
对web.config中2个属性的更改将修复它:
automaticFormatSelectionEnabled=falsedefaultOutgoingResponseFormat=Json (编辑:来自"真实")对我来说,在WebGet属性中将响应格式设置为JSON不起作用.在方法体中设置它;
// This works
WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
return jsonData;
// This doesn't work
`[WebGet(UriTemplate = "/conditions?term={term}", ResponseFormat = WebMessageFormat.Json)]`
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18703 次 |
| 最近记录: |