【问题标题】:WCF 4.0 : WebMessageFormat.Json not working with WCF REST TemplateWCF 4.0:WebMessageFormat.Json 不使用 WCF REST 模板
【发布时间】:2010-09-23 18:44:01
【问题描述】:

this 位置下载了 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" } };
    }

注意 ResponseFormat=WebMessageFormat.Json。这是我对该模板所做的唯一更改。

我错过了什么?

谢谢!

【问题讨论】:

    标签: wcf


    【解决方案1】:

    想通了。标准端点的automaticFormatSelectionEnabled 属性应设置为false,defaultOutgoingReponseFormat 应设置为Json

    <standardEndpoint name="" helpEnabled="true" 
        automaticFormatSelectionEnabled="false" 
        defaultOutgoingResponseFormat ="Json" />
    

    【讨论】:

    • +1 有趣的是,我在 IE 中的 silverlight 应用程序会得到 json,而在 firefox 4 中运行的同一个应用程序会得到 xml。不过,这解决了它。
    • +1 难以置信ResponseFormat = WebMessageFormat.Json 是如何被默默忽略的,你必须弄清楚这一点!如果没有 Google,WCF 将完全无法使用
    • @Andomar 不只是WCF,大部分MS产品都是这样的
    • 在 .NET 4.0 中,我只需要设置 automaticFormatSelectionEnabled="false"。该方法被标记为 ResponseFormat = WebMessageFormat.Json 并且它有效。无需设置 defaultOutgoingResponseFormat
    • 谢谢。这种行为根本不合逻辑。
    【解决方案2】:
     <system.serviceModel>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
            <standardEndpoints>
                <webHttpEndpoint>
                    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
                </webHttpEndpoint>
            </standardEndpoints>
     </system.serviceModel>
    

    对 web.config 中的 2 个属性的更改将修复它:

    • automaticFormatSelectionEnabled=false
    • defaultOutgoingResponseFormat=Json(编辑:来自“真”)

    【讨论】:

      【解决方案3】:

      对我来说,在 WebGet 属性中将响应格式设置为 JSON 不起作用。将其设置在方法的主体中;

      // This works
      WebOperationContext.Current.OutgoingResponse.Format = WebMessageFormat.Json;
      return jsonData;
      
      
      // This doesn't work
      `[WebGet(UriTemplate = "/conditions?term={term}", ResponseFormat = WebMessageFormat.Json)]`
      

      【讨论】:

        【解决方案4】:

        点击->reference links

        "当启用自动格式选择时,基础架构会解析请求消息的 Accept 头,并确定最合适的响应格式。如果 Accept 头没有指定合适的响应格式,基础架构会使用请求消息的 Content-Type请求消息或操作的默认响应格式。"

        编辑:此链接可能会让您继续前进 http://blogs.msdn.com/b/endpoint/archive/2010/11/01/wcf-webhttp-service-returns-http-415-unsupported-media-type.aspx

        【讨论】:

          【解决方案5】:

          每次我尝试创建 JSON Web 服务时都会遇到这样的问题。

          现在,我只需按照此处显示的步骤操作即可。

          http://mikesknowledgebase.com/pages/Services/WebServices-Page1.htm

          它通过屏幕截图和示例逐步展示了如何创建 JSON Web 服务。

          希望这会有所帮助。

          【讨论】:

            猜你喜欢
            • 2011-09-09
            • 1970-01-01
            • 1970-01-01
            • 2011-09-13
            • 2011-10-04
            • 2011-06-29
            • 1970-01-01
            • 2011-08-26
            • 2011-07-21
            相关资源
            最近更新 更多