向web方法发送可变数量的参数(JSON POST?) [英] Sending variable number of parameters to a webmethod (JSON POST?)

查看:65
本文介绍了向web方法发送可变数量的参数(JSON POST?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编写一个微控制器项目,该项目使用以太网控制器(带有TCPIP协议栈)发送HTTP Post请求。



项目的性质意味着要传递给webmethod的参数数量会有所不同(即一些参数名称会有所不同,以及要发送的参数总数)。



我理解一个正常的web方法需要将固定数量的参数传递给web方法。

ie - 下面的webmethod需要传递两个参数。

Hi,
I am writing a microcontroller project which uses an ethernet controller (with a TCPIP Stack) to send HTTP Post requests.

The nature of the project means the number of parameters to be passed to the webmethod will vary (i.e. some of the Parameter names will vary as well as the total number of parameters to be sent).

I understand that a normal webmethod requires a fixed number of arguments to be passed to the webmethod.
i.e - The webmethod below requires TWO parameters to be passed.

[WebMethod]
public string save(string Parameter1, string Parameter2)
{
//Do something
return "Hello World";
}





我知道可以使用重载的webmethods(Messagename)来允许不同数量的[要发送的预定义参数但这似乎不灵活,需要大量重复。



我发现了一段允许XML字符串的代码片段传递给Webmethod作为单个参数(手动解析)。



I am aware that one can use "overloaded" webmethods (Messagename) to allow for a different number of [pre-defined] parameters to be sent but this seems inflexible and would require a lot of duplication.

I discovered a snippet of code that allows for an XML string to be passed to a Webmethod as a single parameter (to be manually parsed).

[WebMethod]
public string save(string saveXml)
{
//parse XML
return "Hello World";
}





我的问题是 - 第二种方法我提到了可接受的做法并且是否有JSON等价物?



注意:我已经为OUTGOING响应发送了JSON,我正在为INCOMING参数寻找JSON解决方案。



My question is - Is the second method I mention acceptable practice and is there a JSON equivalent?

NOTE: I am already sending JSON for the OUTGOING response, I am looking for a JSON solution for INCOMING parameters.

推荐答案

是的,传递xml是一种非常有效的方法。或者,评论中的建议也很好。你也可以传递json。网上有很多样本。
Yes, passing xml is a perfectly valid way of doing it. Or, the suggestion in the comment is fine too. You can also pass json as well. There are lots of samples online.


这篇关于向web方法发送可变数量的参数(JSON POST?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆