ASMX Web服务和键值参数键值、参数、ASMX、Web

2023-09-06 23:49:52 作者:我是流氓我怕谁

目前,我有一个ASMX方法定义如下:

  [WebMethod的]
公共字符串方法一(哈希表的形式)
 

它接收JSON对象有不同数量的属性,例如:

  {形式:{名1:10,名2:20}}
 

这是工作正常,并给出了预期结果调用的时候,但是当我打开浏览器中的Web服务地址,我得到的错误:

  

不支持的System.Collections.Hashtable类型,因为它实现IDictionary的

我试过其他数据类型,如名单,其中,的DictionaryEntry> 这将解决这个问题,但后来为空时调用该方法,而且我找不到东西,将工作在两种情况下...

什么是这样做的正确的方法是什么?

解决方案

的IDictionary 不能被序列化到XML(这是怎么ASMX Web服务的工作),所以你不能用任何实施的IDictionary 无论是作为一个返回值或者作为参数。

所以这样做的正确的方法是使用任何不执行的IDictionary 。你可以做这样的事情,而不是:

  [WebMethod的]
公共字符串方法1(PARAMS KeyValuePair<字符串,字符串> [] FORMDATA)
 
如何完成Goods查询页面 使用Map进行键值对的传入来编写规格参数,StringUtils的分割符用法,json对象的序列化和反序列化,

然后调用它是这样的:

  service.Method1(新KeyValuePair(名1,10),新KeyValuePair(名2,20));
 

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"));