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

Exi*_*tos 10 c# wcf

我有合同:

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

    [WebInvoke(Method = "POST", UriTemplate = "evals")]
    [OperationContract]
    void SubmitVideoPOST(Video videoArg, string userId);
Run Code Online (Sandbox Code Playgroud)

我有实施方法:

public List<Video> GetVideosGET(string userIdArg)
{

  List<Video> catsToReturn = new List<Video>();

  if (Int32.Parse(userIdArg) == 1)
  {
      catsToReturn = catsForUser1;
  }
  else if (Int32.Parse(userIdArg) == 2)
  {
      catsToReturn = catsForUser2;
  }

  return catsToReturn;

  }


  public void SubmitVideoPOST(Video videoArg, string userId)
  {

  }
Run Code Online (Sandbox Code Playgroud)

当我浏览到:

http://localhost:52587/Api/Content/VLSContentService.svc/GetCategoriesGET/1
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

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

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

Les*_*ter 20

看一下这个海报提出同样问题的链接.

相关部分是:

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.
Run Code Online (Sandbox Code Playgroud)

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

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


Ric*_*ett 6

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

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