简体   繁体   English

C#Web服务如何将多个参数发送到POST操作

[英]C# webservice how to send multiple parameters to POST action

I use C# with VS 2015, .NET 4.5.2 and Web API 2 我将C#与VS 2015,.NET 4.5.2和Web API 2结合使用
I want to build a restful webservice. 我想构建一个宁静的Web服务。

I need to use CURL or Fiddler for testing the webservice (not AJAX or Jquery). 我需要使用CURL或Fiddler来测试Web服务(而不是AJAX或Jquery)。
I would prefer CURL if possible. 如果可能,我希望使用CURL。

I succeeded in sending GET requests using CURL to the webservice. 我成功使用CURL向Web服务发送了GET请求。
But I also need to send data to the webservice using POST 但是我还需要使用POST将数据发送到Web服务

Unfortunately I have problems using POST. 不幸的是,我在使用POST时遇到问题。

I need to send not only 1 parameter, but multiple paramters. 我不仅需要发送1个参数,还需要发送多个参数。
(Basically I need to send a whole record that will add to a database) (基本上,我需要发送一条将添加到数据库中的完整记录)

It seems that it is not possible to send multiple parameters to the POST action? 似乎无法将多个参数发送到POST操作?
But instead of sending multiple parameters I should be able to send the record data using an array or List object ? 但是我应该能够使用数组或List对象发送记录数据,而不是发送多个参数?

How should I code the POST action in the webservice controller? 我应该如何在Web服务控制器中编写POST操作代码?
How should I send the data using CURL (or fiddler) so that the POST action can process it? 我应该如何使用CURL(或Fiddler)发送数据,以便POST操作可以处理它?

I saw some examples that have used the URI to POST data... but using URI seems to need content-length to be set? 我看到一些使用URI来发布数据的示例...但是使用URI似乎需要设置内容长度?
If possible I would avoid to set content-length. 如果可能的话,我将避免设置content-length。


Model: 模型:
The model represents the data that should be sent using POST : 该模型表示应使用POST发送的数据:

public class Product
{
public int      Id       { get; set; }
public string   Name     { get; set; }
public string   Category { get; set; }
public decimal  Price    { get; set; }
}



Client: 客户:
The client (CURL/fiddler) must be able to send model data, for example: 客户端(CURL /提琴手)必须能够发送模型数据,例如:

id=5 id = 5
Name="test" 名称=“ test”
Category="Hardware" 类别=“硬件”
Price=10 价格= 10

How can I send this data using CURL (or fiddler) ? 如何使用CURL(或Fiddler)发送此数据?


Controller Action: 控制器动作:
How should I code this action ? 我应该如何编码该动作?
Whould this be OK ? 谁能行吗? :

[HttpPost]
public void AddProduct([FromBody] List<Product> data )
{
 Do something
}


webervice URL webervice URL
Just for completeness: 仅出于完整性考虑:
My webservice URL looks like this 我的网络服务网址如下所示
http://myServer/api/product http:// myServer / api / product

For adding such kind of thing .Make a wrapper class and then send it to POST . 要添加此类内容,请创建包装器类,然后将其发送到POST。

[HttpPost]
public void AddProduct(List<Product> data )
{
 Do something
}

Instead of this what you can do is- Make a Class 除此之外,您可以做的是-上课

class Wrapper{
List<Product> data
}

and then use 然后使用

 [HttpPost]
    public void AddProduct(Wrapper data )
    {
     Do something
    }

This will work . 这将起作用。 If does't work let me know .I will try to provide you the solution . 如果不起作用,请通知我。我将尽力为您提供解决方案。 For me this worked when I was having an issue . 对我来说,这在我遇到问题时起作用。

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

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