【问题标题】:Web API 2 OData = $format not working: the request is always ignoredWeb API 2 OData = $format 不工作:请求总是被忽略
【发布时间】:2020-06-16 15:09:55
【问题描述】:

我有一个 Web API OData 项目,一切正常。我现在尝试使用 $format 参数返回 xml 而不是 JSON,而不是指定标头请求,但它不起作用。我试过这些方法:

http://localhost:3845/api/Customer?$format=application/xml
http://localhost:3845/api/Customer?$format=xml
http://localhost:3845/api/Customer?$format=application/xml;odata.metadata=full

一切都没有成功。这篇文章说有可能:https://blogs.msdn.microsoft.com/webdev/2014/03/13/getting-started-with-asp-net-web-api-2-2-for-odata-v4-0/

我已经更新了我所有的 NuGet 包,但似乎总是忽略该请求,而是每次都得到 JSON。

【问题讨论】:

    标签: asp.net-web-api odata


    【解决方案1】:

    由于 ATOM 格式 (XML) 只是一个技术委员会规范,而不是 OData V4 协议的 OASIS 标准,因此在 version 6.3.0 的 ODataLib 中禁用了 ATOM 格式。

    请求 OData V4 服务以 XML 响应的正确方法如下:

    GET http://localhost:3845/api/Customer?$format=application/atom+xml
    

    或将标题Accept 设置为application/atom+xml。但由于上述原因,它不适用于 Web API OData V4。

    【讨论】:

      【解决方案2】:

      要支持$format=xml$format=json,添加如下配置:

      config.Formatters.JsonFormatter.AddQueryStringMapping("$format", "json", "application/json");
      config.Formatters.XmlFormatter.AddQueryStringMapping("$format", "xml", "application/xml");
      

      【讨论】:

        猜你喜欢
        • 2017-04-02
        • 2013-03-01
        • 2013-04-12
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2018-12-06
        • 2014-04-05
        相关资源
        最近更新 更多