3

I need to send a JavaScript array via querystring to my .ashx. In my .ashx (handler) I'll have a method that will need to parse through each value in the array and populate a generic List

Just trying to think about the way to go about this both passing it to the querystring as well as how to take that querystring value and convert it to a C# array in order to pass that into my method in my .ashx.

So first to pass the array to querystring, I assume it's going to be something like this

var javascriptArray = [1212, 32321, 42342];

now pass it to querystring first iterate through each value in the array and append to a variable in javascript. So I'd end up with something like this

var querystringArray = "1212, 32321, 42342";

and eventually querystringArray will be passed as a querystring parameter to the url that hits my .ashx.

Second, once I grab that parameter, assume I'll just use or create a ultility function that will do a string split and shove it into and return a C# array. Then pass that to my method.

Just wondering if there is an easier or better way to do all this. Thoughts? I'm not the greatest with JavaScript syntax yet.

1
  • My question would be what are you using the HttpHandler for? Is it just to process the Javascript? If it's just for that, have you considered using a combination of JSON and just postback? One tool for JSON on .Net is JSON .Net codeplex.com/Json Commented Jul 1, 2009 at 14:54

1 Answer 1

3

Javascript

var myArray = [12, 34, 56];
var url = "MyHandler.ashx?dat=" + encodeURIComponent(myArray.join());

C#

// In Page_Load
string[] dat = Request.QueryString["dat"].Split(",");
Sign up to request clarification or add additional context in comments.

2 Comments

Nice. I already wrote the split variable on the C# side but did not know you could do .join(). cool.
Yep. By default, the join uses a comma, but you can pass it anything to use, like this: myArray.join(" ") for a space.

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.