Querystrings are one of the simplest ways to pass parameters between pages, to controls or even to filter a simple listing.
Through Razor, this is very easy to do in Contensis, though it can get complicated when dealing with multiple parameters. We’ll look at more complex querystrings later.
In the below example, we’ll imagine we’re filtering a news listing to show only items within a specific category. We will do this by passing the category name as a parameter like so;
To begin with, we need actually check for a querystring;
So, what we’ve done here is declare a variable to hold the querystring value, but set it to be empty.
Then, we check to see if one exists. If it does, then we set the category to the value of that parameter. Otherwise, it stays empty.
So, from here we can do anything we like with that string.
For example, we want to return only news items if they match the category in the query string.
So here, we have set up our main query. Then, we check if we need to filter by category. if we do, then we add this to the query as a subQuery, otherwise we leave it as it is.
When the collection is returned, we should have either a complete list of news items, or a list of items filtered by category, depending on whether we have a querystring parameter called category.
So, that’s how you deal with really simple querystrings. But when if we want to add multiple ones? Well, that’s a little more complex, but its really only a case of getting your subqueries right.