我为什么不能使用两个参数在WCF REST POST方法? [英] Why cant I use two arguments in a WCF REST POST method?

查看:469
本文介绍了我为什么不能使用两个参数在WCF REST POST方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有合同:

    [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);

和我有实现方法:

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)
  {

  }

当我浏览到:

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

即时得到这个错误:

服务器中的/应用程序错误。

合同'IVLSContentService'
指定了多个请求主体
参数,而不
任何包装元素被序列化的操作'SubmitVideoPOST。在大多数一体
参数可以没有
包装元素序列化。要么删除
额外的车身参数或设置在
WebGetAttribute / WebInvokeAttribute裹

BodyStyle属性。

Server Error in '/' Application. Operation 'SubmitVideoPOST' of contract 'IVLSContentService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

我才开始变得对GET请求这个错误,当我增加了对POST(我还没有尝试访问),新方法,这是什么意思?我不能使用一个以上的参数

I only started getting this error on the Get request when I added the new method for POST (which I havent tried to access), what does this mean? Cant I use more than one argument?

推荐答案

看看这个的链接那里的海报询问了同样的问题。

Take a look at this link where the poster asks the same question.

相关部分是:

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.



所以你的情况你必须你的经营合同更改为以下内容:

So in your case you'd have to change your operation contract to the following:

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

这篇关于我为什么不能使用两个参数在WCF REST POST方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆