Let's say I have a web page that currently accepts a single ID value via a url parameter:
http://example.com/mypage.aspx?ID=1234
I want to change it to accept a list of ids, like this:
http://example.com/mypage.aspx?IDs=1234,4321,6789
So it's available to my code as a string via context.Request.QueryString["IDs"]. What's the best way to turn that string value into a List<int>?
Edit: I know how to do .split() on a comma to get a list of strings, but I ask because I don't know how to easily convert that string list to an int list. This is still in .Net 2.0, so no lambdas.
No offense to those who provided clear answers, but many people seem to be answering your question instead of addressing your problem. You want multiple IDs, so you think you could this this:
http://example.com/mypage.aspx?IDs=1234,4321,6789
The problem is that this is a non-robust solution. In the future, if you want multiple values, what do you do if they have commas? A better solution (and this is perfectly valid in a query string), is to use multiple parameters with the same name:
http://example.com/mypage.aspx?ID=1234;ID=4321;ID=6789
Then, whatever query string parser you use should be able to return a list of IDs. If it can't handle this (and also handle semi-colons instead of ampersands), then it's broken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With