C# 如何调用具有多个参数的 Post api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15134555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:12:00  来源:igfitidea点击:

How to call a Post api with multiple parameters

c#asp.net-web-apihttpclient

提问by NewBie

How can i call a Post method with multiple parameters using HttpClient?

如何使用 HttpClient 调用具有多个参数的 Post 方法?

I am using the following code with a single parameter:

我使用带有单个参数的以下代码:

var paymentServicePostClient = new HttpClient();
paymentServicePostClient.BaseAddress = 
                  new Uri(ConfigurationManager.AppSettings["PaymentServiceUri"]);

PaymentReceipt payData = SetPostParameter(card);
var paymentServiceResponse = 
   paymentServicePostClient.PostAsJsonAsync("api/billpayment/", payData).Result;

I need to add another parameter userid. How can i send the parameter along with the 'postData'?

我需要添加另一个参数 userid。如何将参数与“postData”一起发送?

WebApi POST method prototype:

WebApi POST 方法原型:

public int Post(PaymentReceipt paymentReceipt,string userid)

采纳答案by cuongle

UserIdshould be in query string:

UserId应该在查询字符串中:

var paymentServiceResponse = paymentServicePostClient
                            .PostAsJsonAsync("api/billpayment?userId=" + userId.ToString(), payData)
                            .Result;

回答by Darin Dimitrov

Simply use a view model on your Web Api controller that contains both properties. So instead of:

只需在包含这两个属性的 Web Api 控制器上使用视图模型。所以而不是:

public HttpresponseMessage Post(PaymentReceipt model, int userid)
{
    ...
}

use:

用:

public HttpresponseMessage Post(PaymentReceiptViewModel model)
{
    ...
}

where the PaymentReceiptViewModelwill obviously contain the useridproperty. Then you will be able to call the method normally:

其中PaymentReceiptViewModel显然包含的userid属性。然后就可以正常调用该方法了:

var model = new PaymentReceiptViewModel()
model.PayData = ...
model.UserId = ...
var paymentServiceResponse = paymentServicePostClient
    .PostAsJsonAsync("api/billpayment/", model)
    .Result;

回答by SethMW

In my case my existing ViewModels don't line up very nicely with the data I want to post to my WebAPI. So, instead of creating an entire new set of model classes, I posted an anonymous type, and had my Controller accept a dynamic.

在我的情况下,我现有的 ViewModel 与我想要发布到我的 WebAPI 的数据并没有很好地对齐。因此,我没有创建一整套新的模型类,而是发布了一个匿名类型,并让我的 Controller 接受了一个动态类型。

var paymentServiceResponse = paymentServicePostClient.PostAsJsonAsync("api/billpayment/", new { payData, userid }).Result;



public int Post([FromBody]dynamic model)
{
    PaymentReceipt paymentReceipt = (PaymentReceipt)model.paymentReceipt;
    string userid = (string)model.userid;

    ...

}

(I'd be curious to hear some feedback on this approach. It's definitely a lot less code.)

(我很想听听关于这种方法的一些反馈。它的代码肯定少了很多。)