ASP.Net/C# convert NameValueCollection to IDictionary?
Asked Answered
J

3

9

I’m trying to help my son upgrade a web site I built for him last year. He wants to implement 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#. 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. 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.

The transaction has to be “signed” and Amazon provides a SignatureUtils class to accomplish this. 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. What I have to pass it is this ListParams NameValueCollection that I built with:

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. 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>'
Jesher answered 24/4, 2013 at 20:54 Comment(2)
Take a look at: https://mcmap.net/q/920512/-how-to-convert-namevaluecollection-to-hashtable Seems like the answer has the code to get things to a IDictonaryEmit
Steven, That post should work for me, but the .ToDictionary is giving me an inaccessibility error due to its protection level. It's Public, so I'm not sure what is causing that error.Jesher
J
1

Shazzam!! Got it! The solution was to change the sample code presented by Hajan to implement a Dictionary rather than a NameValueCollection. Then I needed to change is while loop in the PaymentGatewayPost to a foreach loop as follows:

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.

Jesher answered 25/4, 2013 at 14:0 Comment(0)
R
8

You can do that by using Cast:

IDictionary<string, string> dict = ListParams.Cast<string>()
    .ToDictionary(p => p, p => ListParams[p]);
Rawden answered 24/4, 2013 at 21:6 Comment(3)
mattytommo, this solution is very similar to the one proposed by Steven above. As I attempt to implement it, I'm told that the NameValueCollection does not contain a definition for "Cast" I'm going to try to cast it differently, and have also been considering simply rewriting the code that creates the NameValueCollection. That may also be an option.Jesher
@Jesher Add a using reference to System.LinqRawden
Thanks mattytommo. I was just reading an online article last week about Linq. It's something I've not used yet, but sounds very powerful, and worth my learning how to take advantage of.Jesher
G
6

You can also "convert" a NameValueCollection to Dictionary<string, string> by going through its AllKeys property:

var dictionary = nameValueCollection.AllKeys
    .ToDictionary(k => k, k => nameValueCollection[k]);
Gallstone answered 1/12, 2016 at 23:42 Comment(0)
J
1

Shazzam!! Got it! The solution was to change the sample code presented by Hajan to implement a Dictionary rather than a NameValueCollection. Then I needed to change is while loop in the PaymentGatewayPost to a foreach loop as follows:

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.

Jesher answered 25/4, 2013 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.