为什么我不能在WCF REST POST方法中使用两个参数?

我有合同:

[OperationContract] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetCategoriesGET/{userIdArg}", BodyStyle = WebMessageBodyStyle.Bare)] List 

我有实施方法:

 public List 

当我浏览到:

 http://localhost:52587/Api/Content/VLSContentService.svc/GetCategoriesGET/1 

我得到这个错误:

‘/’应用程序中的服务器错误。 合同’IVLSContentService’的操作’SubmitVideoPOST’指定要序列化的多个请求体参数,而不包含任何包装元素。 最多可以在没有包装元素的情况下序列化一个body参数。 删除额外的body参数或将WebGetAttribute / WebInvokeAttribute上的BodyStyle属性设置为Wrapped。

当我添加POST的新方法(我还没有尝试访问)时,我才开始在Get请求中收到此错误,这是什么意思? 我不能使用多个参数吗?

看看这个海报提出同样问题的链接 。

相关部分是:

 WCF doesn't support more than one parameter with bare body, if you need pass several parameters in one post method operation, then we need set the BodyStyle to Wrapped. 

因此,在您的情况下,您必须将操作合同更改为以下内容:

 [WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.WrappedRequest)] [OperationContract] void SubmitVideoPOST(Video videoArg, string userId); 

XML不会有一个带有两个参数的根节点,这会导致它不正确。 要引入单个根节点,必须按照错误说明,“包装”它。 这使得该方法期望围绕两个数据的包装元素

将BodyStyle = WebMessageBodyStyle.Wrap添加到WebInvoke属性

您是否尝试将WebGetAttribute / WebInvokeAttribute上的BodyStyle属性设置为Wrapped,如同建议的错误,如下所示:

 [WebInvoke(Method = "POST", UriTemplate = "evals", BodyStyle = WebMessageBodyStyle.Wrapped)] [OperationContract] void SubmitVideoPOST(Video videoArg, string userId); 

我自己对WCF REST有些新意,上周刚刚完成了我的第一次服务。 但我有类似的问题。 这篇文章让我朝着正确的方向前进。 包装是我的问题。