Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL Querystring - Find, replace, add, update values?

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.

like image 525
schooner Avatar asked Jul 22 '09 08:07

schooner


People also ask

How can I add or update a query string parameter?

set('PARAM_HERE', VALUE_HERE); history. pushState(null, '', url); This will preserve everything about the URL and only change or add the one query param. You can also use replaceState instead of pushState if you don't want it to create a new browser history entry.

What is request QueryString ()?

The value of Request. QueryString(parameter) is an array of all of the values of parameter that occur in QUERY_STRING. You can determine the number of values of a parameter by calling Request. QueryString(parameter).


1 Answers

Yes 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.

like image 156
Rob Levine Avatar answered Oct 21 '22 10:10

Rob Levine