views:

1135

answers:

7

Hello

I have a URL that also might have a query string part, the query string might be empty or have multiple items.

I want to replace one of the items in the query string or add it if the item doesn't already exists.

I have an URI object with the complete URL.

My first idea was to use regex and some string magic, that should do it.

But it seems a bit shaky, perhaps the framework has some query string builder class?

A: 

You can speed up RegExps by precompiling them.

Check out this tutorial

majkinetor
Performance is not my concern here
Karsten
+2  A: 

No, the framework doesn't have any existing QueryStringBuilder class, but usually the querystring information in a HTTP request is available as an iterable and searchable NameValueCollection via the Request.Querystring property.

Since you are starting off with a Uri object, however, you will need to obtain the querystring portion using the Query property of the Uri object. This will yield a string of the form:

Uri myURI = new Uri("http://www.mywebsite.com/page.aspx?Val1=A&Val2=B&Val3=C");
string querystring = myURI.Query;

// Outputs: "?Val1=A&Val2=B&Val3=C". Note the ? prefix!
Console.WriteLine(querystring);

You can then split this string on the ampersand character to differentiate it into different querystring parameters-value pairs. Then again split each parameter on the "=" character to differentiate it into a key and value.

Since your final goal is to search for a particular querystring key and if necessary create it, you should try to (re)create a collection (preferably, a generic one) that allows you easily search in the collection, similar to the facility provided by the NameValueCollection class.

Cerebrus
A: 

A simple check and replace:

    If strTest.Contains("?somesearchstring") Then
        'replace query string part
        strTest.Replace("querystringpart", "replacementvalue")
    Else
        'add the query string part
        strTest = String.Concat(strTest, "querysting")
    End If
Tanner
This won't work if the "somesearchstring" is not the first querystring parameter, will it?
Cerebrus
I guess not, the '?' was to demonstrate that the querystring value check would be carried out there.
Tanner
A: 

I answered a similar question a while ago. Basically, the best way would be to use the class HttpValueCollection, which the QueryString property actually is, unfortunately it is internal in the .NET framework. You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you.

HttpValueCollection extends NameValueCollection, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString() method to later rebuild the query string from the underlying collection.

hmemcpy
+2  A: 

Maybe you could use the System.UriBuilder class. It has a Query property.

knut
+1  A: 

I agree with Cerebrus. Sticking to the KISS principle, you have the querystring,

string querystring = myURI.Query;

you know what you are looking for and what you want to replace it with.

So use something like this:-

if (querystring == "") 
  myURI.Query += "?" + replacestring; 
else 
  querystring.replace (searchstring, replacestring); // not too sure of syntax !!
Steve
A: 
string link = page.Request.Url.ToString();

if(page.Request.Url.Query == "")
    link  += "?pageIndex=" + pageIndex;
else if (page.Request.QueryString["pageIndex"] != "")
{
    var idx = page.Request.QueryString["pageIndex"];
    link = link.Replace("pageIndex=" + idx, "pageIndex=" + pageIndex);
}
else 
    link += "&pageIndex=" + pageIndex;

This seems to work really well.

Damian