views:

2272

answers:

5

We inherited some C# code as part of a project from another company which does URL redirects that modifies the existing query string, changing values of items, adding new params, etc as needed. The issue however is that the code is buggy at best, and ends up duplicating items in the query string instead of updating them properly. The code works on the first pass but on additional calls the duplication issues become apparent.

Ex: MyPage.aspx?startdate=08/22/09&startdate=09/22/09

Instead of duplicating the item it needs to be either updated with the new value if it already exists, or added if not there already.

Is there a C# class or set of functions for handling query strings, allowing a simple means to access and update/add parameters that gets around these issues instead of the blind add approach that seems to be in use now with the code? This needs to be able to handle multiple parameters that may or may not exists at all times and be added and updated on subsequent calls.

We would sooner use existing logic than recreate something if possible so as to get this resolved quickly in a semi standard way for future maintainability and reuse.

A: 

Hi,

I hope this helps.

Barbaros Alp
+1  A: 

You can access and manipulate all values of your Querystring through the Request.QueryString collection. Here's a link.

Traveling Tech Guy
I think this is read-only though, you can't change the values in Request.QueryString directly
tjrobinson
+4  A: 

I would suggest converting the querystring to a collection by using

HttpUtility.ParseQueryString()

You can then find/add/update/replace values directly in the collection, before re-creating the querystring from this collection.

This should make it easier to spot duplicates.

Rob Levine
How would you recommend re-creating the QueryString from the NameValueCollection?
tjrobinson
I can think of a few code approaches, but they won't really fit in a comments box. This is StackOverflow - ask it as a question - I'm sure you'll get several answers :D
Rob Levine
http://blog.leekelleher.com/2008/06/06/how-to-convert-namevaluecollection-to-a-query-string/
David Liddle
@ tjrobinson `var qsCol = HttpUtility.ParseQueryString(Request.Url.Query); querystring = qsCol.ToString();`
Dan Diplo
A: 

this seems a basic design problem.

instead of updating the current query string, what SHOULD be done is simply adding all the parameters to the base at every time.

sure, you CAN update it, but (pseudocode)

if querystring exists
  then update query string
else
  add query string

will get crazy when you start using more than 1 variable.

redesign would be best, effort allowing.

b0x0rz
A: 

The WCF REST Starter Kit available on ASP.NET also include a new "HttpQueryString" helper class that will most likely be included in the .NET 4.0 time frame into the base class library.

See an excellent screencast on how to use this utility class here:

http://channel9.msdn.com/shows/Endpoint/endpointtv-Screencast-HttpClient-Query-String-and-Form-Input-Management/

Marc

marc_s