2

I'm making this oddball webservice that gets its parameters as query parameters in the URL. Problem is, that a single parameter can have multiple values. Similar to:

http://mydomain/Service?operation=ActivateUser&id=1&id=2&id=3

In this case I need to activate 3 users with ID's 1, 2 and 3. Even worse, it could be like this (I think, the spec is a bit vague, will clarify later):

http://mydomain/Service?operation=ActivateUser&id=1&operation=DeleteUser&id=2

In this case I need to activate user 1 and delete user 2.

So I really need a URL parser which preserves the order of the parameters. .NET's default Request.QueryString doesn't do that, it just separates the values by commas and to hell with order.

Short of diving in and splitting the raw URL by ?, & and = myself, is there a better way?

2
  • Could you change delimiters? eg. op=ActivateUser&ids=1|2|3? Commented Nov 4, 2011 at 14:44
  • No, unfortunately this representation is forced upon me. Commented Nov 4, 2011 at 14:53

1 Answer 1

3

It sounds like Request.QueryString.GetValues (which is NameValueCollection.GetValues)is what you're after. It will return a string array of the values it finds for a given key and in the order they are in the url..

Given http://www.site.com/page.aspx?keyid=1&keyid=2&keyid=3

string[] keys = Request.QueryString.GetValues("keyid");

keys[0] would be "1"
keys[1] would be "2"
keys[2] would be "3"
Sign up to request clarification or add additional context in comments.

4 Comments

Nice! The only problem now could be in my second example. Will see next week if it is a real problem or not.
@Vilx- Should be okay still. You'd have to check separately for the operation querystring, and then I assume you would act upon the 2nd item in the array.
But if there are 2 values for the "operation" and 4 values for "id", how do I know which ids go to which operation?
@Vilx- Ahh. Gotcha. Didn't read that as a possibility with multiple operations. That one may be more difficult. May have to manually parse at that point.

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.