【问题标题】:Odata Query with DataServiceContext and get result as json使用 DataServiceContext 查询 Odata 并以 json 格式获取结果
【发布时间】:2023-04-03 00:54:01
【问题描述】:

我有一个 DataServiceContext 查询我的 WCF 数据服务:

service = new DataServiceContext(new Uri(SvcUrl));
service.SendingRequest += new EventHandler<SendingRequestEventArgs>
(OnSendingRequest);

我想设置 Accept 标头,以便我得到 json 格式的结果:

    static void OnSendingRequest(object sender, SendingRequestEventArgs e)
    {
        e.RequestHeaders.Set("Accept", "json/text");
    }

但我得到一个例外:

"必须使用适当的属性或方法修改此标头。 参数名称:名称"

我似乎无法弄清楚如何设置 Accept 标头。

【问题讨论】:

  • 如果您有 DataServiceContext 为您处理它,为什么要指定格式?你会看到它吗?不过,例外情况可能更具体......

标签: wcf odata wcf-data-services


【解决方案1】:

呜呜呜……刚刚找到这个msdn thread

那里的答案是:

“由于客户端代码不是为了处理 json 响应而编写的,因此我们不允许覆盖 Accept 标头。最好的办法是直接使用 HttpWebRequest,然后自己处理 json 响应。”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-12
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2015-01-19
    • 2014-07-23
    相关资源
    最近更新 更多