Search code examples
c#query-stringnamevaluecollection

NameValueCollection not grabbing first value of query string


I am using a simple statement

NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString);

I am trying to grab the values passed in the query string. A simple example of something passed would be

form_id=webform_client_form_4&referrer=http://myURL/webform/request-information?agent=agent&keyword=keyword&full_name=Jon Harding&company=RTS Financial - TEST&phone=913-555-5555

The issue I am having is that the referrer is actually the full string after it. I do need the other values. In this case becauase the ParseQueryString splits the string at the & character the first value is always ignored in the NameValueCollection

Currently when I try to get the value of the agent it is a blank value, understandably.

What would be the best method to make sure I grab all variables? I could split the string at the question mark and then prepend an ampersand to the string before I do the ParseQueryString. Is there a more elegant want to do this?


Solution

  • This will work with least effort:

    NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString.Replace("?","&"));