c# - OData 和 WCF : Unable to perform POST, 放置和删除

标签 c# wcf visual-studio wcf-data-services odata

我已经使用 this 配置并构建了 WCF 和 oData with Visual Studio 2012教程。

所以我使用了Firefox REST 客户端插件来测试它是否工作正常。

这是我处理 json 的基本 URL,192.168.1.4/TestdataService/TestDataService.svc/User?$format=json

我可以成功地执行GET 操作。但是,我无法执行POST、PUT 和 DELETE 操作。

POST 操作说

Status Code: 415 Unsupported Media Type

它的响应体

 {
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "Unsupported media type requested."
    }
  }
}

PUT & DELETE 操作说

Status Code: 405 Method

Not Allowed Allow: GET, POST

它的响应体

{
  "odata.error": {
    "code": "",
    "message": {
      "lang": "en-US",
      "value": "The URI 'http://192.168.1.4/TestdataService/TestDataService.svc/User?$format=json' is not valid for DELETE operation. The URI must refer to a single resource from an entity set or from a property referring to a set of resources."
    }
  }
}

我附上下面的屏幕截图。对此的任何帮助表示赞赏。

谢谢。

获取 enter image description here

发布 enter image description here

enter image description here

enter image description here

放置 enter image description here

enter image description here

enter image description here

最佳答案

我认为您这里有两个不相关的问题。 PUTDELETE 动词必须引用特定的实体,而不是实体集。当您 PUTTestDataService.svc/User 时,这就像尝试更新整个实体集,这是不允许的。相反,您需要指定要更新的实体(例如,通过 PUT-ting 到 TestDataService.svc/User(0),或者键查找你的实体集)。

POST 问题是不同的。在 POST 情况下,您应该以实体集而不是单个实体为目标。您遇到的错误与请求的 Content-TypeAccept header 有关。现在(为了简化调试),我会在 URI 中保留 $format=json 参数,因为它会覆盖 Accept header 。尝试在 POST 请求中包含以下 header 值:

Accept: application/json
Content-Type: application/json
DataServiceVersion: 3.0;
MinDataServiceVersion: 3.0;
MaxDataServiceVersion: 3.0;

如果您仍然遇到错误,请告诉我们。

同样,这个 POST 请求应该发送到 TestDataService.svc/User,就像您最初那样。 PUTDELETE 应指向单个实体(例如,TestDataService.svc/User(0))

此外,正如 qujck 在上面的评论中指出的那样,在您的屏幕截图中,PUT 和 POST 的请求正文是空的。 POST 的意思是“插入这个条目”,所以你需要提供一个条目来插入到请求的正文中。 PUT 表示“替换/更新此条目”,因此在这种情况下您也需要向服务器提供数据。

关于c# - OData 和 WCF : Unable to perform POST, 放置和删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010578/

相关文章:

c# - 直接调用 anon async Func 与使用 Task.Factory 之间的区别?

c# - WPF 行为 + Storyboard动画 = AssociatedObject 为 null

c# - 为什么使用 ImmutableList 而不是 ReadOnlyCollection?

c++ - MS Visual Studio Windows 中的 Release模式与 Debug模式

c# - 如何隐式转换这个?

c# - 为什么从 WCF 服务生成的引用在项目之间不同?

c# - WCF,如何实例化 DataContract 中的所有对象? [OnDeserializing] 不起作用

.net - 在所有连接上将 ServicePointManager.SecurityProtocol 强制为 TLS 1.2

visual-studio - 在 WinJS 中更改我的 UWP 应用程序中的窗口边框颜色

c++ - 加速大量数组相关计算,visual studio