Kevin Randriajaoson
Kevin Randriajaoson

Reputation: 121

Parse Query containing comma ","

I have a form where it's possible to have a name with multiple values (e.g: <input name="name1" type="text"> and there's is another input with the same name name1). And when I want to get the data in a handler, I use the method HttpUtility.ParseQueryString() and then I put it in a var nameValue so when I want to get the value, I do nameValue["name1"]. The problem is that when I get the value of name1 I get "name1_value1,name1_value2" with the comma as separator, so I need to split it to get "name1_value1""name1_value2" separately, but what if name1_value1 contained actually a comma (,) then there's the problem. What I did is transform context.Request.Params into string, then replace all the commas (encoded to %2c in params) with ;;; and replace them again when I finished the splitting but I'm not sure this would work properly because maybe there is another special character which contains %2c

Upvotes: 0

Views: 1236

Answers (1)

Farzin Kanzi
Farzin Kanzi

Reputation: 3435

As you said nameValue["name1"] will join the result by a comma and that is what you don't want. So it is better to forget the HttpUtility.ParseQueryString() and parse the query string by on your own.

string queryString = url.Split('?')[1];
string[] qsArray = queryString.Split('&');
var name1 = qsArray.ToList().Where(A => A.StartsWith('name1'));

Now you have your values totally separated.

Upvotes: 2

Related Questions