问题
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?
回答1:
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"));
回答2:
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...
来源:https://stackoverflow.com/questions/3149525/asmx-web-services-and-key-value-parameters