asmx Web 服务和键值参数

发布于 2024-09-07 11:22:05 字数 488 浏览 2 评论 0原文

我目前有一个像这样定义的 asmx 方法:

[WebMethod]
public String Method1(Hashtable form)

它接收具有可变数量属性的 json 对象,例如:

{"form":{"name1":"10","name2":"20"}}

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

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

我尝试过其他数据类型,例如 List 它将修复此问题,但在调用方法时为空,并且我找不到在这两种情况下都有效的东西......

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生戏 2024-09-14 11:22:05

IDictionary 无法序列化为 XML(这就是 asmx Web 服务的工作方式),因此您不能使用 IDictionary 的任何实现作为返回值或参数。

因此,“正确”的方法是使用任何未实现 IDictionary 的方法。你可以这样做:

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

然后像这样调用它:

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

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"));
老娘不死你永远是小三 2024-09-14 11:22:05

目前,我可以将其作为解决方法:

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

并且服务 .asmx 页面加载时不会出现错误。

但这对我来说仍然没有意义......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文