【问题标题】:OData update controller throwing 415 Unsupported media type errorOData 更新控制器抛出 415 Unsupported media type 错误
【发布时间】:2014-02-27 07:51:00
【问题描述】:

我有一个带有“Put”和“Post”操作的 OData 控制器。当我发布 Content-Type:application/json 时,我收到一个带有“415:不支持的媒体类型”的 HttpResponseException。控制器不会抛出异常的唯一 Content-Typeapplication/x-www-urlformencoded。但这不是我想要接受的内容类型。

为什么 odata 控制器不接受 application/json?我在控制器的配置中看不到任何限制接受的内容类型的内容。

我记得过去使用相同的代码。 Soemthing似乎已经退步了。我应该去哪里看?

【问题讨论】:

    标签: asp.net rest asp.net-web-api odata


    【解决方案1】:

    感觉您期望在正文中的类型(即在您的操作中声明为参数)不是您服务的 EDM 模型的一部分。

    例如,

    HttpResponseMessage Post(Customer customer)
    

    如果客户不是您的 EDM 模型的一部分,Web API 会抛出 415。这是因为 ODataControllers 只有 ODataMediaTypeFormatter 而 ODataMediaTypeFormatter 只支持读取模型中声明的类型。

    【讨论】:

    • 这可能是它!非常感谢你!我尝试使用 ODataModelBuilder.EntitySet 方法将类型添加到模型中,但出现以下模型状态错误,“实体类型 'CustomerUpdate' 与提供的实体集 'Container.Customer' 的基本类型 'CustomerDTO' 不兼容。当为 OData 提要或条目阅读器指定实体类型时,它必须与指定实体集的基本类型相同或子类型。"
    • 我已经有一个用于 Customer 控制器的 CustomerDTO 类型,现在想要添加一个 CustomerUpdate 类型(用于 POST)。
    猜你喜欢
    • 2012-04-19
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    相关资源
    最近更新 更多