【问题标题】:How to remove "d" wrapper from WCF Data Service using EF4如何使用 EF4 从 WCF 数据服务中删除“d”包装
【发布时间】:2011-10-28 15:45:31
【问题描述】:

我的问题与JsonConvert.DeserializeObject and "d" wrapper in WCF 类似,但我不确定如何针对我的情况实施此问题。这是我完整的 web.config WCF 数据服务:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<location path="MyWcfDataService.svc">
    <system.web>
      <authorization>
        <allow users="?"/>
      </authorization>
    </system.web>

    <system.serviceModel>
      <behaviors>
        <endpointBehaviors>
          <behavior name="MyWcfDataServiceBehavior">
            <webHttp defaultOutgoingResponseFormat="Json"/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>
</location>

</configuration>

我的数据服务很简单:

// specifying WebMessageBodyStyle.Bare doesn't seem to have any effect
[WebGet(ResponseFormat = WebMessageFormat.Json, 
    RequestFormat = WebMessageFormat.Json, 
    BodyStyle=WebMessageBodyStyle.Bare)] 
public string GetJsonTest(string x)
{

    var json = new
    {
        hello = "world"
    };

    return new JavaScriptSerializer().Serialize(json);
}

【问题讨论】:

    标签: .net json c#-4.0 wcf-data-services


    【解决方案1】:

    为了扩展达西的答案,d 对象对于防范跨站点脚本攻击是必要的。 http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx

    【讨论】:

      【解决方案2】:

      问题是您自己在返回值中生成 json,然后要求 WCF 将生成的 json 字符串作为 json 返回。

      您至少有两种可能的选择:

      1) 不要指定 WCF 将其响应值格式化为 JSON 并保持您的代码不变(您承担在每个方法中生成 json 的责任)。

      2) 修改您的方法以返回您希望在 json 中生成的类的实例(wcf 承担生成 json 的责任)。为此,您需要将类声明移出方法。

      【讨论】:

      • 1) 返回完全相同的内容。 { "d" : { "GetJsonTest": "{\"hello\":\"world\"}" } } 2) 我正在使用 EF,所以这个解决方案既不简单也不实用。
      【解决方案3】:

      据我所知,删除“d”是不可能的。出于安全原因,它内置于数据服务中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-12
        • 1970-01-01
        • 2011-07-06
        • 1970-01-01
        • 1970-01-01
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多