如何删除“d”使用 EF4 来自 WCF 数据服务的包装器
我的问题与此类似 JsonConvert.DeserializeObject 和 WCF 中的“d”包装器< /a> 但我不确定如何针对我的情况实施此操作。这是我完整的 WCF 数据服务 web.config:
<?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);
}
My question is similar to this one JsonConvert.DeserializeObject and "d" wrapper in WCF but I'm not sure how to implement this for my situation. Here is my complete web.config the WCF Data Service:
<?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>
My Data Service is simple enough:
// 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,删除“d”是不可能的。出于安全原因,它被内置到数据服务中。
As far as I can tell, it is impossible to remove the 'd'. It is built into Data Services for security reasons.
问题是您自己在返回值中生成 json,然后要求 WCF 将生成的 json 字符串作为 json 返回。
您至少有两个可能的选择:
1) 不要指定 WCF 将其响应值格式化为 JSON 并保留代码不变(您承担在每个方法中生成 json 的责任)。
2) 修改您的方法以返回您想要以 json 形式生成的类的实例(wcf 承担生成 json 的责任)。要实现此目的,您需要将类声明移出方法。
The issue is that you are generating the json yourself in the return value, then asking WCF to return your generated json string as json.
You have at least two possible options:
1) Do not specify that WCF formats its response values as JSON and leave your code as is (you assume the responsibility for generating the json in every method).
2) Modify your method to return an instance of the class that you want generated in json (wcf assumes the responsibility for generating the json). To accomplish this, you would need to move the class declaration out of the method.
为了扩展达西的答案,d 对象对于防范跨站点脚本攻击是必要的。 http://haacked.com/archive /2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx
To expand on Darcy's answer, the d object is necessary to guard against cross site scripting attacks. http://haacked.com/archive/2008/11/20/anatomy-of-a-subtle-json-vulnerability.aspx