0

I want to pass array of string to my controller. I pass my data from another c# code.

Here my get request :

String[] varList = new List<String> { "A", "B" }.ToArray();
using (var client = new HttpClient())
{
    await client.GetAsync(GetHttpSchema() + HttpContext.Current.Request.Url.Host + "/AController/B?varA=" + varA + "&varB=" + varB + "&varList=" + varList);
}

And here my controller:

public partial class AController : BaseController
{
    public async Task B(string A, string B, String[] varList) { }
}

Now in debug i get the following value(Only one value) for varList : System.String[]..... Any suggestions?

5
  • No... The expected result i a new array with two values, A and B. Commented Aug 6, 2015 at 13:09
  • then why you adding a array to a string? "&varList=" + varList Commented Aug 6, 2015 at 13:10
  • so how i should pass my array? Commented Aug 6, 2015 at 13:11
  • Probably as a delimited string that you split at the other end Commented Aug 6, 2015 at 13:12
  • What format is expected? Comma separated? JSON? Commented Aug 6, 2015 at 13:12

1 Answer 1

1

First off you can just create an array like this

string[] varList = new [] {"A", "B"}

Second you want to get the values of the array, but the ToString method for arrays is the default which just gives you the name of the type. Instead you can use string.Join

string listOfValues = string.Join(",", varList);

That will give you a comma separated list of your values. Or you might need to do something like the following to get "&varList=A$varList=B" based on this.

string listOfValues = "$varList=" + string.Join("&varList=", varList);

Personally I don't know what format you need those values in on your http request, so I'll leave that part up to you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. About the array i know, i just wanted a quick change here in stack. I used your string.Join approach and this work great !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.