Level: 200
 

Working with query strings in Sitecore

In .NET there is no dedicated class for working with query strings. So adding, removing or changing query string parameters requires lots of code with check for parameters existing, placement in the query string and so on.


Fortunately Sitecore has made working with query strings very easy with the UrlString class. In this article I will demonstrate how to use it to manipulate query strings.  I will start the article by describing what query strings are and when to use them.

Written by: Jimmi Lyhne Andersen
Fri, Aug 27 2010

In a url the query string is the part that follows the question mark. It consist of a series of name/value pairs separated by ampersands, with the name and values being separated by an equal signs like this:


http://www.domain.com/path/to/item.aspx?name1=value1&name2=value2


In the next sections I will demonstrate every part that goes into manipulating a query strings, but first I will run through why and when they should be used.

 

Why and when to use query strings

As everyone working with the web knows http is a stateless protocol. But we do not want a page to look the same every time it is visited. The page could contain a paginated list, that should only show the part of the list chosen by the page number. It could also be a search result page, where the list of result should be defined by the search term entered by the user. An example could also be a webshop or a theme chosen in the users profile.


There are several ways to manage these situations. This could be done by using query strings, session variables or cookies to mention a few.

 

So when should we use query strings to manage the state?

We should do that, when the user should be able to bookmark the page in its state or send it to a friend by email. In these situation query strings are the only possibility.
Unlike cookies or session variables, query strings will also work when the user has disabled cookies in her browser.
Query strings are also scalable as they do not consume server resources and there is no problem when moving the site to a webfarm enviroment.

 

When should query strings not be used?

As query strings, unlike session variables, only contains strings of a limited size, they should only be used to pass relative short strings.
As the values in a query string is visible clear text, they should not contain sensitive data.

 

How to use query strings

Query strings should be hackable. By that I mean that it should be obvious to the user how to go to a different page in a paginated control, just by modifying the query string values.
As it is very easy to modify the values in a query string, you should always make sure that the values passed are valid before using them to query a database etc.

 

Creating the UrlString

The UrlString class is located in the Sitecore.Text namespace. It has three constructors. The first one being an empty constructor.
To create a UrlString from the current url we do the following:

UrlString url = new Sitecore.Text.UrlString(Request.Url.AbsoluteUri);

 

It is also possible to create it from a NameValueCollection like this.

NameValueCollection nvcol = new NameValueCollection();
nvcol.Add("name1", "value1");
UrlString url = new UrlString(nvcol);

 

Getting parameter values


To get a parameter value from the UrlString, use the Parameters property like this

url.Parameters["name1"]

This will return the value of the parameter named name1 if it exist and empty string if name1 does not exist.

 

Adding or replacing parameter values


The UrlString class includes a couple of methods for adding and setting parameter values.

      UrlString.Add(string key, string value)
    

This method will add the specified value to the specified key. The value can be an empty string.
If you do not want to allow empty strings as values you should add false as the third parameter for the method.


The Add method will return the url as a string. If you do not need the url, but only want to add a new value or replace an existing value, you should use the Append method

      UrlString.Append(string key, string value)
    

You should not set values by using the Parameters property, as this will use the value as is. Both the Add and Append methods will url encode the values.

 

If the key already exist in the query string, both Add and Append will just replace the existing value with the new one.

 

Removing parameters

To remove a parameter, use the method

      UrlString.Remove(string key);
    

This will remove the parameter if it exist, if it doesn't exist the Remove method does nothing.
The Remove method also returns the url as a string.

 

Please rate this article


7 rates / 4,57 avg.

  • Picture of Jimmi Lyhne Andersen

    About the author:

    Jimmi Lyhne Andersen

    Jimmi Lyhne Andersen is the co-founder of LearnSitecore, and is a partner at Inmento Solutions.
    Jimmi has been lead developer and architect on several large public websites with 200+ editors, and is therefore focused on usability and accessibility.
    Jimmi is a certified Sitecore developer and a Microsoft Certified Professional Developer (MCPD).
    He has a bachelors degree in Computer Science and in Mathematics from the University of Copenhagen.

     

2 responses to "Working with query strings in Sitecore"

Thank you, since im new in the SiteCore universe i found this article very useful.
Posted: Wednesday, October 27, 2010 9:10 AM
With a controller, the querystring processing should not be necessary. As long as the method takes parameters that match the querystring parm names exactly, it will parse them automatically.
Posted: Tuesday, May 02, 2017 3:22 PM

Leave a reply


Notify me of follow-up comments via email.
 
 
#nbsp;