【问题标题】:How do I return Json serialized data from WCF?如何从 WCF 返回 Json 序列化数据?
【发布时间】:2013-07-15 16:06:46
【问题描述】:

我环顾四周,发现可以从 WCF Web 服务返回序列化为 Json 的对象。有谁知道我该怎么做?

谢谢

【问题讨论】:

  • 确保您的请求客户端正在发送“Accept: application/json”标头?
  • 你可以看到下面的链接:[return-clean-json-from-a-wcf-service][1] [1]:stackoverflow.com/questions/2086666/…

标签: c# json wcf serialization


【解决方案1】:

你必须像这样为服务添加属性

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    ObjectName YourMethodName();

【讨论】:

  • @Funky 这将返回序列化为 JSON 的对象。你能详细说明一下吗?
【解决方案2】:

是的,您可以在 web.config 中将 automaticFormatSelectionEnabled 设置为 webHttpEndpoint 的 true standardEndpoint,例如

<webHttpEndpoint>
  <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>

并且您需要为您的客户端的 json 响应添加 http 标头

using (HttpClient client = new HttpClient("endpoint"))
{
     HttpRequestMessage request = new HttpRequestMessage("GET", "SomeMethod");                
     request.Headers.Accept.AddString("application/json");
    ...
}

【讨论】:

  • 你能提供这个教程或链接吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
相关资源
最近更新 更多