开发者

asmx web services and key value parameters

开发者 https://www.devze.com 2023-01-05 09:59 出处:网络
I currently have an asmx method defined like so : [WebMethod] public String Method1(Hashtable form) It receives json objects with a variable number of attributes, for example :

I currently have an asmx method defined like so :

[WebMethod]
public String Method1(Hashtable form)

It receives json objects with a variable number of attributes, for example :

开发者_开发百科
{"form":{"name1":"10","name2":"20"}}

This is working fine and gives the expected results when called, but when I open the web service address in the browser, I get the error :

The type System.Collections.Hashtable is not supported because it implements IDictionary

I've tried other data types, like List<DictionaryEntry> which will fix this, but then be empty when the method is called, and I can't find something that will work in both cases...

What is the "correct" way of doing this?


IDictionary cannot be serialized to XML (which is how asmx web services work), so you cannot use any implementation of IDictionary either as a return value or as a parameter.

So the "correct" way of doing this is to use anything that doesn't implement IDictionary. You could do something like this instead:

[WebMethod]
public String Method1(params KeyValuePair<string, string>[] formdata)

and then call it like this:

service.Method1(new KeyValuePair("name1", "10"), new KeyValuePair("name2", "20"));


For the time being, I can do this as a workaround :

[WebMethod]
public String Method1(Object form)
{
    Dictionary<String, Object> data = (Dictionary<String, Object>)form;

And the service .asmx page loads without error.

This still makes no sense to me though...

0

精彩评论

暂无评论...
验证码 换一张
取 消