将整数传递给FormUrlEncodedContent

时间:2016-08-07 11:25:52

标签: c# asp.net-mvc

我使用asp.net mvc和c#, 我的银行服务项目我使用此代码进行银行业务

var requestContent = new FormUrlEncodedContent(new[] {

    new KeyValuePair<string, string>("amount", payment.Amount), //i want change this section for <string,int>
    new KeyValuePair<string, string>("transid", "id"),
    new KeyValuePair<string, string>("pin","pin" )

});

var client = new HttpClient();

HttpResponseMessage response = await client.PostAsync("http://address.com/api/verify/", requestContent);

HttpContent responseContent = response.Content;

string result = "";

using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
    result = await reader.ReadToEndAsync();
}

现在我的银行场景是变化,我应该传递金额的整数,我怎么能从mycode做我? 因为FormUrlEncodedContent接受<string,string> 谢谢你的帮助

2 个答案:

答案 0 :(得分:0)

你需要使用类似Amount.ToString()的东西,因为这个内容最终会被作为POST主体的一部分进行字符串/编码。

答案 1 :(得分:0)

试试这个,它对我有用。

        public class PaymentActionsRequest
        {
            [JsonProperty("payment_intent")]
            public string PaymentIntent { get; set; }
            
            [JsonProperty("amount")]
            public long Amount { get; set; }        
        }

      var keyValueContent = PaymentActionsRequest.ToKeyValue();
      var formUrlEncodedContent = new FormUrlEncodedContent(keyValueContent);