C# 将字符串数组传递给 webservice 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12991703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:14:45  来源:igfitidea点击:

Passing string array to webservice method

c#asp.netvisual-studio-2010web-servicesasmx

提问by user1754598

I have a web service with this method:

我有一个使用这种方法的网络服务:

[WebMethod]
public int[] stringTest(string[] tString)
{

    int numberOfStrings = tString.Length;
    int[] returnS = new int[numberOfStrings];
    for (int i = 0; i <= numberOfStrings; i++)
    { 
        returnS[i] = 1;

    }
    return returnS;
}

And then I'm trying to pass an array of strings to it from a client program as following:

然后我试图从客户端程序向它传递一个字符串数组,如下所示:

var client = new WebServiceSample.WebService1SoapClient();
string[] parameterNames = { "Windsensor","Temperature sensor"};
test = client.stringTest(parameterNames);

But I'm getting these errors:

但我收到这些错误:

The best overloaded method match for 'SoapWebServiceClient.WebServiceSample.WebService1SoapClient.stringTest(SoapWebServiceClient.WebServiceSample.ArrayOfString)' has some invalid arguments

'SoapWebServiceClient.WebServiceSample.WebService1SoapClient.stringTest(SoapWebServiceClient.WebServiceSample.ArrayOfString)'的最佳重载方法匹配有一些无效参数

and

Argument 1: cannot convert from 'string[]' to 'SoapWebServiceClient.WebServiceSample.ArrayOfString'

参数 1:无法从“string[]”转换为“SoapWebServiceClient.WebServiceSample.ArrayOfString”

What is wrong with my code?

我的代码有什么问题?

采纳答案by codingbiz

Try this

尝试这个

SoapWebServiceClient.WebServiceSample.ArrayOfString arrString = SoapWebServiceClient.WebServiceSample.ArrayOfString();

arrString.AddRange(parameterNames);

or

或者

arrString.Add(....); //if that exists

Check these links

检查这些链接

Hope that helps!

希望有帮助!

回答by Alison Nunes

A simple way is:

一个简单的方法是:

In JavaScript build a new array:

在 JavaScript 中构建一个新数组:

var myArray = new Array();
myArray.push([value1, value2,...]);

In C# just create an ICollectionparameter to get your matrix:

在 C# 中,只需创建一个ICollection参数即可获取矩阵:

[WebMethod(EnableSession = true)]
public MyMethod[] GetMatrixFromJavascript(System.Collections.ICollection myArray)
{ 
   ... 
}