Vilx-
Vilx-

Reputation: 107002

How to parse a URL string with multiple parameter values?

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?

Upvotes: 2

Views: 4825

Answers (1)

Doozer Blake
Doozer Blake

Reputation: 7797

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"

Upvotes: 3

Related Questions