【问题标题】:ResponseFormat returns 0 byteResponseFormat 返回 0 字节
【发布时间】:2014-09-21 17:19:53
【问题描述】:

我想制作一个带有 JSON 返回格式的 REST Web 服务。实际上它适用于简单的字符串返回类型,但是当我将自定义类型作为返回值时,我收到 0 字节。

如果我将 ResponseFormat 更改为 XML,它可以正常工作

感谢您的帮助!

编辑 - 添加示例

界面:

[WebGet(UriTemplate="WebGetTry/{param1}/{param2}", ResponseFormat=WebMessageFormat.Json)]
WebGetType WebGetTry(string param1, string param2);

实现:

public WebGetType WebGetTry(string param1, string param2) {
  return new WebGetType();
}

之后我的测试很简单,就是从 IE 中调用地址。返回页面无法显示错误

【问题讨论】:

  • 请向我们展示一些您用于返回响应的代码。
  • 谢谢,编辑了原帖。

标签: c# json rest


【解决方案1】:

您需要指定 JSON 端点的实现。请检查您的 web.config 并添加以下内容:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
        <webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
  </webHttpEndpoint>
</standardEndpoints>

【讨论】:

  • 谢谢,但这并不是真正的解决方案,我仍然没有 json 结果,我认为最好的方法是下载 WCF REST 项目模板link 或从 Web API 开始
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 2012-04-15
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多