Dealing with Mulitple (same key) Query String items in C#

Today i bumped into a problem that was quite easy in the (good old) days of traditional ASP.

Our webapplication needed to pass multiple id’s to a page in it’s query string. The query string was built by simply “joining” a list of id’s and passing that into the url.

var QString =String.Join("&id=", item.OrderConfirmationData.VendorOrders.Select(x => x.VendorOrderNumber))

This would build a query string that looked something like

Reading it back via server side code using somehting like to string returned a comma seperated string. (that might not have been bad if some of the values had comma’s in them. :)

e.g. 1,23,43,103

This inturn caused us to split the results to and array based on the comma. arghhh that was when the fun started.

So.. the solution.

The querystring object in .net has a .GetValues method. If you pass in the Parameter name, it return a string array of values..

At that point parcing the values is easy and clean. e.g.

var values = Request.QueryString.GetValues("id");
foreach (var item in values)
litSPLIT.Text += item + "<br />";