[英]ASP.Net/C# convert NameValueCollection to IDictionary?
I'm trying to help my son upgrade a web site I built for him last year. 我正在努力帮助我的儿子升级我去年为他建造的网站。 He wants to implement Amazon Simple Pay.
他想实施Amazon Simple Pay。 I'm SO close to getting it, but have an error that I don't know how to address.
我很接近它,但有一个错误,我不知道如何解决。 It's an ASP.Net site done in C#.
这是一个用C#完成的ASP.Net站点。 I am an untrained (self-taught) developer, so speak in simple terms, please.
我是一名未受过培训的(自学成才的)开发人员,请用简单的语言说。 ;-)
;-)
In ASP.Net, it is not legal to have a Form within a Form, and I need to do a Form POST. 在ASP.Net中,在表单中使用表单是不合法的,我需要进行表单POST。 There is a pretty slick tutorial online that shows how to make this happen.
网上有一个非常漂亮的教程,展示了如何实现这一目标。 The URL is http://weblogs.asp.net/hajan/archive/2011/04/20/amazon-simple-pay-in-asp-net.aspx if you're interested in seeing it.
如果您对此感兴趣, 请访问http://weblogs.asp.net/hajan/archive/2011/04/20/amazon-simple-pay-in-asp-net.aspx 。
The transaction has to be “signed” and Amazon provides a SignatureUtils class to accomplish this. 该事务必须“签名”,Amazon提供SignatureUtils类来完成此任务。 In that class, I'm calling this:
在那堂课中,我称之为:
public static string signParameters(IDictionary<String, String> parameters, String key, String HttpMethod, String Host, String RequestURI, String algorithm)
What's killing me is the IDictionary parameter. 杀死我的是IDictionary参数。 What I have to pass it is this ListParams NameValueCollection that I built with:
我必须传递的是我使用以下构建的ListParams NameValueCollection:
public System.Collections.Specialized.NameValueCollection ListParams = new System.Collections.Specialized.NameValueCollection();
It's giving me the error below, because it can't convert the NameValueCollection to an IDictionary. 它给出了下面的错误,因为它无法将NameValueCollection转换为IDictionary。 I tried explicitly converting it, but no joy.
我试着明确转换它,但没有快乐。 How can I solve this?
我怎么解决这个问题?
Error: Argument 1: cannot convert from 'System.Collections.Specialized.NameValueCollection' to 'System.Collections.Generic.IDictionary<string,string>'
You can do that by using Cast
: 你可以使用
Cast
来做到这一点:
IDictionary<string, string> dict = ListParams.Cast<string>()
.ToDictionary(p => p, p => ListParams[p]);
You can also "convert" a NameValueCollection
to Dictionary<string, string>
by going through its AllKeys
property: 您还可以通过遍历其
AllKeys
属性将NameValueCollection
“转换”为Dictionary<string, string>
:
var dictionary = nameValueCollection.AllKeys
.ToDictionary(k => k, k => nameValueCollection[k]);
Shazzam!! Shazzam! Got it!
得到它了! The solution was to change the sample code presented by Hajan to implement a Dictionary rather than a NameValueCollection.
解决方案是更改Hajan提供的示例代码以实现Dictionary而不是NameValueCollection。 Then I needed to change is while loop in the PaymentGatewayPost to a foreach loop as follows:
然后我需要将PaymentGatewayPost中的while循环更改为foreach循环,如下所示:
foreach (KeyValuePair<string, string> pair in ListParams)
{
System.Web.HttpContext.Current.Response.Write(string.Format("<input name=\"{0}\" type=\"hidden\" value=\"{1}\">",
pair.Key,
pair.Value));
}
Voila!! 瞧! It builds without error.
它构建没有错误。
Thanks everyone for your help. 谢谢大家的帮助。 Hopefully this will help others who are struggling with the Amazon Simple Pay.
希望这将有助于其他正在努力应对亚马逊简单支付的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.