简体   繁体   English

具有单个参数的WCF RESTful服务的XML发布请求应该是什么样?

[英]What should an XML post request look like for WCF RESTful service with a single parameter?

I am having trouble working out what my XML should look like when performing a post request through WCF REST services. 通过WCF REST服务执行发布请求时,我很难确定XML的外观。 When using a datacontract i have no problem at all but when i just want to send across one parameter for example an int, i get the following error - "The remote server returned an error: (405) Method Not Allowed. " 使用数据合同时,我一点都没有问题,但是当我只想发送一个参数(例如int)时,我收到以下错误-“远程服务器返回错误:(405)不允许的方法。”

[OperationContract]  
[WebInvoke(UriTemplate = "/DeleteUser", Method= "Post")]  
bool DeleteUser(int userId);

What should my XML look like? 我的XML应该是什么样?

Thanks in advance 提前致谢

DJ -- The only way I've found to do what you were originally asking is to swipe the example from this link. DJ-我发现做您最初要求的唯一方法是从此链接中滑动示例。 It uses a Stream class to bring in the post parameters in the body of the HTTP request. 它使用Stream类在HTTP请求的主体中引入post参数。 Then you have to slog through it manually... 然后,您必须手动浏览它...

Hope this helps. 希望这会有所帮助。

like this 像这样

[OperationContract]
[WebInvoke(UriTemplate = "/DeleteUser/{userId}", Method= "Post")]
bool DeleteUser(string userId)
{
   int actualUserId = Int32.Parse(userId);
   ...
}

ps: Why are you using POST with a single parameter? ps:为什么要使用带有单个参数的POST?


I can see not using GET if you want to Delete, but then why not use HTTP DELETE ? 如果要删除,我可以看到没有使用GET,但是为什么不使用HTTP DELETE? In this case the URI Template would be /user/{userId} and the Method = "Delete". 在这种情况下,URI模板将为/ user / {userId},并且Method =“ Delete”。

There's no payload, no XML to pass. 没有有效载荷,没有XML可以传递。

Rob Bagby explains Rob Bagby解释

the code would look like 代码看起来像

[OperationContract]
[WebInvoke(UriTemplate = "/User/{userId}", Method= "Delete")]
bool DeleteUser(string userId)
{
   int actualUserId = Int32.Parse(userId);
   ...
}

Im using a post as its a delete function so i dont want to use a parameter in the uri. 我使用帖子作为其删除功能,所以我不想在uri中使用参数。 Get calls should be for getting data. 获取呼叫应该用于获取数据。

Thats not the problem. 那不是问题。 The userId is an int and is expecting and int. userId是一个int,并且是期望和int。 My question is if doing a post using one parameter what should the xml look like? 我的问题是,如果使用一个参数进行发布,那么xml应该是什么样子?

Make sure you are performing a POST operation to the resource. 确保对资源执行POST操作。 The URL might be as follows: 该URL可能如下:

http://localhost/SampleApplication/DeleteUser

Below is the request format that you need to have 以下是您需要的请求格式

<int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">55</int>  

The above xml needs to be part of the message body. 上面的xml必须是消息正文的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM